Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions db/week-1/sql/-- Exercise 4 -to - 5.pgsql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- Exercise 4
--4.1
SELECT * FROM reservations WHERE checkout_date - checkin_date >0;

--4.2

SELECT rate*5 AS "5 Nights No Discount", rate*0.15*5 AS "Discount", rate*5 - rate*0.15*5 AS "Discounted Rate for 5 Nights"
FROM rooms
WHERE room_type ILIKE 'premier' OR room_type ILIKE 'premier plus';

--4.3
select * from reservations
where checkin_date between '2023-01-01' and '2023-02-01';


-- Exercise 5

--5.1
SELECT DISTINCT room_type FROM rooms;

--5.2
SELECT name,address,phone FROM customers ORDER BY name;

--5.3
SELECT name, address,city,country FROM customers ORDER BY country, city DESC;

--5.4
--SELECT room_no,room_type,rate*5 AS "Cost of 5 Nights" FROM rooms ORDER BY rate*5 DESC LIMIT 15; -- this query will work as same the following one.
SELECT room_no,room_type,rate*5 AS "Cost of 5 Nights" FROM rooms ORDER BY "Cost of 5 Nights" DESC LIMIT 15;
50 changes: 50 additions & 0 deletions db/week-1/sql/--Exercise 1 -to- 3.pgsql
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
--Exercise 1
--1.1 List the name, phone , eamil of all customers
SELECT name,phone,email FROM customers;

--1.2 List all the details of rooms
SELECT * FROM rooms;

--1.3 List the customersid, checkin date and number of guests from reservations
SELECT cust_id,checkin_date,no_guests FROM reservations;

-- Exercise 2
--2.1 \d customers this is how to display a table definition in the terminal
--2.2 \? select will show all options available under the SELECT command.
--2.3 \d is one of the informational commands and it shows the list of SEQUENCES
cyf_hotels=> \ds
List of relations
Schema | Name | Type | Owner
--------+---------------------+----------+----------------
public | bookings_id_seq | sequence | codeyourfuture
public | customers_id_seq | sequence | codeyourfuture
public | hotels_id_seq | sequence | codeyourfuture
public | invoices_id_seq | sequence | codeyourfuture
public | reservations_id_seq | sequence | codeyourfuture
(5 rows)

-- Exercise 3
--3.1 customers from Norway
SELECT * FROM customers WHERE country="Norway";

--3.2 rooms that can accommodate more then 2 no_guests
SELECT * FROM rooms WHERE no_guests > 2;

--3.3 invocies dated back a month ago.
SELECT * FROM invoices WHERE invoice_date <='2023-01-02';

--3.4 the effect of 15% discount of last MONTH
SELECT * FROM invoices WHERE invoice_date >= '2023-01-01'; --to see the table data with same period

SELECT sum(total) As "Total Without Discount",
sum(total)*0.15 AS "Total Discount",
sum(total)-sum(total)*0.15 AS "Discounted Total"
from invoices
where invoice_date >= '2023-01-01';

--3.5 List of customers names starts with "W"

SELECT * FROM customers WHERE name ILIKE 'm%';