-
Notifications
You must be signed in to change notification settings - Fork 44
DB week_1 homework #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,9 +7,18 @@ Below you will find a set of tasks for you to complete to consolidate and extend | |
| To submit this homework write the correct commands for each question here: | ||
|
|
||
| ```sql | ||
|
|
||
|
|
||
| ``` | ||
| 1- select * from rooms where rate >100; | ||
| 2-select * from reservations where checkin_date between '2020-09-01' and '2020-09-30' and checkout_date - checkin_date >3; | ||
| 3-select * from customers where city like 'M%'; | ||
| 4-insert into room_types( room_type, def_rate) values ('PENTHOUSE', 185.00); | ||
| 5-insert into rooms( room_no, room_type, rate) values (501,'PENTHOUSE', 185.00), (502,'PENTHOUSE', 185.00); | ||
| 6-insert into rooms( room_no, room_type, rate) values (503,'PREMIER PLUS', 143.00); | ||
| 7- select count(room_no) | ||
| from reservations where checkin_date between '2020-08-01'and '2020-08-31' | ||
| 8- select sum(checkout_date - checkin_date) from reservations where room_no between 201 and 299; | ||
| 9- select count(*), sum(total), avg(total) from invoices where total >300; | ||
| 10- select sum(checkout_date - checkin_date) from reservations group by room_no/100 ; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very elegant solution! One more advice, it could be made more information by adding a floor number to the query. |
||
| ``` | ||
|
|
||
| When you have finished all of the questions - open a pull request with your answers to the `Databases-Homework` repository. | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,18 +33,62 @@ Open the file `cyf_ecommerce.sql` in VSCode and examine the SQL code. Take a pie | |
| ## Task | ||
|
|
||
| Once you understand the database that you are going to work with, solve the following challenge by writing SQL queries using everything you learned about SQL: | ||
|
|
||
| 1. Retrieve all the customers' names and addresses who live in the United States | ||
| select name, address from customers where country ='United States'; | ||
|
|
||
| 2. Retrieve all the customers in ascending name sequence | ||
| select * from customers order by name ; | ||
|
|
||
| 3. Retrieve all the products whose name contains the word `socks` | ||
| select * from products where product_name like '%socks%'; | ||
|
|
||
| 4. Retrieve all the products which cost more than 100 showing product id, name, unit price and supplier id. | ||
| select a.prod_id, p.product_name, a.unit_price, a.supp_id from products p join product_availability a on (a.prod_id = p.id) where a.unit_price >100 ; | ||
|
|
||
| 5. Retrieve the 5 most expensive products | ||
| select * from product_availability order by unit_price DESC limit 5; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's fine but would be nice to see product info as well. |
||
|
|
||
| 6. Retrieve all the products with their corresponding suppliers. The result should only contain the columns `product_name`, `unit_price` and `supplier_name` | ||
| select p.product_name, a.unit_price, s.supplier_name from products p join product_availability a on (a.prod_id = p.id) join suppliers s on (a.supp_id = s.id) ; | ||
|
|
||
| 7. Retrieve all the products sold by suppliers based in the United Kingdom. The result should only contain the columns `product_name` and `supplier_name`. | ||
| select p.product_name, s.supplier_name from products p join product_availability a on(a.prod_id = p.id) join suppliers s on (a.supp_id = s.id) where s.country = 'United Kingdom'; | ||
|
|
||
| 8. Retrieve all orders, including order items, from customer ID `1`. Include order id, reference, date and total cost (calculated as quantity * unit price). | ||
|
|
||
| select o.order_reference, o.order_date from orders o join order_items i on (i.order_id = o.id) join product_availability a on (a.prod_id = i.product_id) where o.customer_id = 1; | ||
| ////////////I don't understand what they exactly want from this question //////////////// | ||
|
Comment on lines
+59
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's break it down
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're on the right track. Just need to add some extra column such as order id and the figure to calculate to end price by grouping the same order. |
||
|
|
||
| 9. Retrieve all orders, including order items, from customer named `Hope Crosby` | ||
| select o.* from orders o join customers c on (o. customer_id = c.id) where c.name = 'Hope Crosby'; | ||
| ////// what do yo mean by including order items //////////////////////////// | ||
|
Comment on lines
62
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume order items refers to the |
||
|
|
||
| 10. Retrieve all the products in the order `ORD006`. The result should only contain the columns `product_name`, `unit_price` and `quantity`. | ||
| select p.product_name , a.unit_price, i.quantity from orders o join order_items i on(i.order_id = o.id) join product_availability a on (i.supplier_id = a.supp_id) join products p on (p.id = a. prod_id) | ||
| where o.order_reference = 'ORD006'; | ||
|
Comment on lines
+67
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be more intuitive joining the product availability through the product table and also add a join condition for connecting product on product availability table ( e.g. |
||
|
|
||
| 11. Retrieve all the products with their supplier for all orders of all customers. The result should only contain the columns `name` (from customer), `order_reference`, `order_date`, `product_name`, `supplier_name` and `quantity`. | ||
| select c.name, o.order_reference, o.order_date, s.supplier_name , i.quantity | ||
| from customers c join | ||
| orders o on(c.id= o.customer_id) join | ||
| order_items i on (o.id = i. order_id) join | ||
| products p on (i.product_id = p.id) join | ||
| suppliers s on (s.id =i.supplier_id); | ||
|
|
||
| 12. Retrieve the names of all customers who bought a product from a supplier based in China. | ||
| select c.name from customers c join orders o on(c.id= o.customer_id) join | ||
| order_items i on (o.id = i. order_id) join | ||
| suppliers s on (s.id =i.supplier_id) | ||
| where s.country = 'China'; | ||
|
Comment on lines
+79
to
+82
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's okay but as an improvement, it is worth adding a |
||
|
|
||
|
|
||
| 13. List all orders giving customer name, order reference, order date and order total amount (quantity * unit price) in descending order of total. | ||
| select c.name, o.order_reference, o.order_date, sum (quantity * unit_price) as total | ||
| from customers c join orders o on(c.id= o.customer_id) join | ||
| order_items i on (o.id = i. order_id) join | ||
| product_availability a on (i.supplier_id = a.supp_id) | ||
| group by c.name, o.order_reference, o.order_date | ||
| ORDER BY totaL DESC ; | ||
|
Comment on lines
+86
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basically, you did it here what hadn't understood here:
The only problem with the query is the same as the aforementioned. |
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| node_modules |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| drop table if exists order_items; | ||
| drop table if exists orders; | ||
| drop table if exists customers; | ||
| drop table if exists products; | ||
| drop table if exists suppliers; | ||
|
|
||
| CREATE TABLE customers ( | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(50) NOT NULL, | ||
| address VARCHAR(120), | ||
| city VARCHAR(30), | ||
| country VARCHAR(20) | ||
| ); | ||
|
|
||
| CREATE TABLE suppliers ( | ||
| id SERIAL PRIMARY KEY, | ||
| supplier_name VARCHAR(100) NOT NULL, | ||
| country VARCHAR(20) NOT NULL | ||
| ); | ||
|
|
||
| CREATE TABLE products ( | ||
| id SERIAL PRIMARY KEY, | ||
| product_name VARCHAR(100) NOT NULL, | ||
| unit_price INT NOT NULL, | ||
| supplier_id INT REFERENCES suppliers(id) | ||
| ); | ||
|
|
||
| CREATE TABLE orders ( | ||
| id SERIAL PRIMARY KEY, | ||
| order_date DATE NOT NULL, | ||
| order_reference VARCHAR(10) NOT NULL, | ||
| customer_id INT REFERENCES customers(id) | ||
| ); | ||
|
|
||
| CREATE TABLE order_items ( | ||
| id SERIAL PRIMARY KEY, | ||
| order_id INT REFERENCES orders(id), | ||
| product_id INT REFERENCES products(id), | ||
| quantity INT NOT NULL | ||
| ); | ||
|
|
||
| /////////////////////homework solution //////////////////////// | ||
| 1-SELECT name, address FROM customers WHERE country ='United States'; | ||
|
|
||
| 2-SELECT * FROM customers ORDER BY name ; | ||
|
|
||
| 3-SELECT * FROM products WHERE unit_price > 100; | ||
|
|
||
| 4-SELECT * FROM products WHERE product_name LIKE '%socks%'; | ||
|
|
||
| 5-SELECT * FROM products | ||
| ORDER BY unit_price DESC | ||
| limit 5; | ||
|
|
||
| 6-SELECT products.product_name, products.unit_price, supplier_name FROM products | ||
| INNER JOIN suppliers ON products.supplier_id = suppliers.id ; | ||
|
|
||
| 7-SELECT products.product_name, supplier_name FROM products | ||
| INNER JOIN suppliers ON products.supplier_id = suppliers.id WHERE suppliers.country = 'United Kingdom'; | ||
| 8- SELECT * FROM orders WHERE id = 1; | ||
|
|
||
| 9-SELECT orders.* , customers.name FROM orders | ||
| INNER JOIN customers ON orders.customer_id = customers.id WHERE customers.name = 'Hope Crosby'; | ||
|
|
||
| 10-SELECT products.product_name, products.unit_price, order_items.quantity ,orders.order_reference FROM order_items | ||
| INNER JOIN orders ON order_items.order_id = orders.id | ||
| INNER JOIN products ON order_items.product_id = products.id | ||
| WHERE orders.order_reference = 'ORD006'; | ||
|
|
||
| 11-SELECT customers.name, orders.order_reference, orders.order_date, products.product_name, suppliers.supplier_name , order_items.quantity FROM | ||
| customers INNER JOIN orders ON orders.customer_id = customers.id | ||
| INNER JOIN order_items ON order_items.order_id = orders.id | ||
| INNER JOIN products ON order_items.product_id = products.id | ||
| INNER JOIN suppliers ON products.supplier_id = suppliers.id; | ||
|
|
||
| 12- SELECT DISTINCT customers.name FROM | ||
| customers INNER JOIN orders ON orders.customer_id = customers.id | ||
| INNER JOIN order_items ON order_items.order_id = orders.id | ||
| INNER JOIN products ON order_items.product_id = products.id | ||
| INNER JOIN suppliers ON products.supplier_id = suppliers.id | ||
| WHERE suppliers.country = 'China'; | ||
| //////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| INSERT INTO customers (name, address, city, country) VALUES ('Guy Crawford','770-2839 Ligula Road','Paris','France'); | ||
| INSERT INTO customers (name, address, city, country) VALUES ('Hope Crosby','P.O. Box 276, 4976 Sit Rd.','Steyr','United Kingdom'); | ||
| INSERT INTO customers (name, address, city, country) VALUES ('Britanney Kirkland','P.O. Box 577, 5601 Sem, St.','Little Rock','United Kingdom'); | ||
| INSERT INTO customers (name, address, city, country) VALUES ('Amber Tran','6967 Ac Road','Villafranca Asti','United States'); | ||
| INSERT INTO customers (name, address, city, country) VALUES ('Edan Higgins','Ap #840-3255 Tincidunt St.','Arles','United States'); | ||
| INSERT INTO customers (name, address, city, country) VALUES ('Quintessa Austin','597-2737 Nunc Rd.','Saint-Marc','United Kingdom'); | ||
|
|
||
| INSERT INTO suppliers (supplier_name, country) VALUES ('Amazon', 'United States'); | ||
| INSERT INTO suppliers (supplier_name, country) VALUES ('Taobao', 'China'); | ||
| INSERT INTO suppliers (supplier_name, country) VALUES ('Argos', 'United Kingdom'); | ||
| INSERT INTO suppliers (supplier_name, country) VALUES ('Sainsburys', 'United Kingdom'); | ||
|
|
||
| INSERT INTO products (product_name, unit_price, supplier_id) VALUES ('Tee Shirt Olympic Games', 20, 1); | ||
| INSERT INTO products (product_name, unit_price, supplier_id) VALUES ('Tee Shirt Olympic Games', 18, 2); | ||
| INSERT INTO products (product_name, unit_price, supplier_id) VALUES ('Tee Shirt Olympic Games', 21, 3); | ||
| INSERT INTO products (product_name, unit_price, supplier_id) VALUES ('Mobile Phone X', 299, 1); | ||
| INSERT INTO products (product_name, unit_price, supplier_id) VALUES ('Mobile Phone X', 249, 4); | ||
| INSERT INTO products (product_name, unit_price, supplier_id) VALUES ('Super warm socks', 10, 1); | ||
| INSERT INTO products (product_name, unit_price, supplier_id) VALUES ('Super warm socks', 5, 2); | ||
| INSERT INTO products (product_name, unit_price, supplier_id) VALUES ('Super warm socks', 8, 3); | ||
| INSERT INTO products (product_name, unit_price, supplier_id) VALUES ('Super warm socks', 10, 4); | ||
| INSERT INTO products (product_name, unit_price, supplier_id) VALUES ('Le Petit Prince', 10, 1); | ||
| INSERT INTO products (product_name, unit_price, supplier_id) VALUES ('Le Petit Prince', 10, 4); | ||
| INSERT INTO products (product_name, unit_price, supplier_id) VALUES ('Ball', 14, 1); | ||
| INSERT INTO products (product_name, unit_price, supplier_id) VALUES ('Ball', 15, 4); | ||
| INSERT INTO products (product_name, unit_price, supplier_id) VALUES ('Ball', 20, 2); | ||
| INSERT INTO products (product_name, unit_price, supplier_id) VALUES ('Javascript Book', 40, 1); | ||
| INSERT INTO products (product_name, unit_price, supplier_id) VALUES ('Javascript Book', 39, 3); | ||
| INSERT INTO products (product_name, unit_price, supplier_id) VALUES ('Javascript Book', 41, 2); | ||
| INSERT INTO products (product_name, unit_price, supplier_id) VALUES ('Coffee Cup', 3, 1); | ||
| INSERT INTO products (product_name, unit_price, supplier_id) VALUES ('Coffee Cup', 4, 2); | ||
| INSERT INTO products (product_name, unit_price, supplier_id) VALUES ('Coffee Cup', 4, 3); | ||
| INSERT INTO products (product_name, unit_price, supplier_id) VALUES ('Coffee Cup', 5, 4); | ||
|
|
||
| INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-06-01', 'ORD001', 1); | ||
| INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-07-15', 'ORD002', 1); | ||
| INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-07-11', 'ORD003', 1); | ||
| INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-05-24', 'ORD004', 2); | ||
| INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-05-30', 'ORD005', 3); | ||
| INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-07-05', 'ORD006', 4); | ||
| INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-04-05', 'ORD007', 4); | ||
| INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-07-23', 'ORD008', 5); | ||
| INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-07-24', 'ORD009', 5); | ||
| INSERT INTO orders (order_date, order_reference, customer_id) VALUES ('2019-05-10', 'ORD010', 5); | ||
|
|
||
| INSERT INTO order_items (order_id, product_id, quantity) VALUES(1, 2, 1); | ||
| INSERT INTO order_items (order_id, product_id, quantity) VALUES(1, 7, 5); | ||
| INSERT INTO order_items (order_id, product_id, quantity) VALUES(2, 8, 4); | ||
| INSERT INTO order_items (order_id, product_id, quantity) VALUES(2, 11, 1); | ||
| INSERT INTO order_items (order_id, product_id, quantity) VALUES(3, 20, 10); | ||
| INSERT INTO order_items (order_id, product_id, quantity) VALUES(3, 14, 2); | ||
| INSERT INTO order_items (order_id, product_id, quantity) VALUES(4, 4, 1); | ||
| INSERT INTO order_items (order_id, product_id, quantity) VALUES(5, 16, 2); | ||
| INSERT INTO order_items (order_id, product_id, quantity) VALUES(5, 10, 1); | ||
| INSERT INTO order_items (order_id, product_id, quantity) VALUES(6, 19, 3); | ||
| INSERT INTO order_items (order_id, product_id, quantity) VALUES(6, 17, 1); | ||
| INSERT INTO order_items (order_id, product_id, quantity) VALUES(6, 11, 1); | ||
| INSERT INTO order_items (order_id, product_id, quantity) VALUES(6, 9, 3); | ||
| INSERT INTO order_items (order_id, product_id, quantity) VALUES(7, 8, 15); | ||
| INSERT INTO order_items (order_id, product_id, quantity) VALUES(8, 1, 1); | ||
| INSERT INTO order_items (order_id, product_id, quantity) VALUES(8, 5, 1); | ||
| INSERT INTO order_items (order_id, product_id, quantity) VALUES(9, 13, 2); | ||
| INSERT INTO order_items (order_id, product_id, quantity) VALUES(10, 14, 1); | ||
| INSERT INTO order_items (order_id, product_id, quantity) VALUES(10, 6, 5); | ||
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More precisely
count(distinct room_no), otherwise, you will get how many times rooms were occupied.distinctkeyword removes duplicates before counting so that we will get how many different rooms were occupied.