Skip to content
This repository was archived by the owner on Aug 17, 2024. It is now read-only.
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 4 additions & 2 deletions 2-exercises/cyf_ecommerce.sql
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
drop table if exists order_items;
drop table if exists orders cascade;
// import the data from the csv files
drop table if exists order_items;
drop table if exists orders cascade; //
DROP TABLE IF EXISTS product_availability cascade;
drop table if exists customers cascade;
drop table if exists products cascade;
Expand DownExpand Up@@ -122,3 +123,4 @@ INSERT INTO order_items VALUES (17, 9, 6, 4, 2);
INSERT INTO order_items VALUES (18, 10, 6, 2, 1);
INSERT INTO order_items VALUES (19, 10, 4, 1, 5);


58 changes: 58 additions & 0 deletions 2-exercises/task.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,16 +35,74 @@ Open the file `cyf_ecommerce.sql` in VSCode and examine the SQL code. Take a pie
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 ASC;
3. Retrieve all the products whose name contains the word `socks`
SELECT * FROM products WHERE lower(product_name) LIKE '%socks%';
4. Retrieve all the products which cost more than 100 showing product id, name, unit price and supplier id.

SELECT products.id, products.product_name, product_availability.unit_price, product_availability.supp_id
FROM products
INNER JOIN product_availability
ON (products.id = product_availability.prod_id)
WHERE product_availability.unit_price > 100;
5. Retrieve the 5 most expensive products
SELECT products.id, products.product_name, product_availability.unit_price
FROM products
INNER JOIN product_availability
ON (products.id = product_availability.prod_id)
ORDER BY product_availability.unit_price DESC
LIMIT 5;
6. Retrieve all the products with their corresponding suppliers. The result should only contain the columns `product_name`, `unit_price` and `supplier_name`

SELECT products.product_name, product_availability.unit_price, suppliers.supplier_name
FROM products
INNER JOIN product_availability
ON products.id = product_availability.prod_id
INNER JOIN suppliers
ON product_availability.supp_id = suppliers.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 products.product_name, suppliers.supplier_name
FROM products
INNER JOIN product_availability
ON products.id = product_availability.prod_id
INNER JOIN suppliers
ON product_availability.supp_id = suppliers.id AND suppliers.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 orders.id, orders.order_reference, orders.order_date, order_items.quantity * product_availability.unit_price AS total_cost
FROM orders
INNER JOIN order_items
ON orders.id = order_items.order_id
INNER JOIN product_availability
ON order_items.product_id = product_availability.prod_id
WHERE orders.customer_id = 1;

9. Retrieve all orders, including order items, from customer named `Hope Crosby`

SELECT c.name, o.id, oi.id, oi.quantity, c.address, c.city, c.country FROM customers c JOIN orders o ON (c.id = o.customer_id) JOIN order_items oi ON (o.id = oi.order_id) WHERE c.name = 'Hope Crosby';


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, pa.unit_price, oi.quantity FROM products p JOIN product_availability pa ON (p.id = pa.prod_id) JOIN order_items oi ON (oi.product_id = pa.prod_id) JOIN orders o ON (o.id = oi.order_id) WHERE o.order_reference = 'ORD006';

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, p.product_name, s.supplier_name, oi.quantity FROM customers c JOIN orders o ON (c.id = o.customer_id) JOIN order_items oi ON (o.id = oi.order_id) JOIN suppliers s ON (oi.supplier_id = s.id) JOIN product_availability pa ON (s.id = pa.supp_id) JOIN products p ON (p.id = pa.prod_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 oi ON (o.id = oi.order_id) JOIN suppliers s ON (oi.supplier_id = s.id) WHERE s.country = 'China';


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, oi.quantity * pa.unit_price AS TOTAL_AMOUNT FROM customers c JOIN orders o ON ( c.id = o.customer_id) JOIN order_items oi ON (o.id = oi.order_id) JOIN product_availability pa ON (oi.product_id = pa.prod_id) ORDER BY TOTAL_AMOUNT DESC;

36 changes: 36 additions & 0 deletions 3-projects/server.js
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
const express = require("express");
const app = express();
const { Pool } = require("pg");

const db = new Pool({
user: "nabwa",
host: "localhost",
database: "cyf_ecommerce",
password: "",
port: 5432,
});

app.get("/customers", function (req, res) {
db.query("SELECT * FROM customers", (error, result) => {
res.json(result.rows);
});
});

app.get("/suppliers", function (req, res) {
db.query("SELECT * FROM suppliers", (error, result) => {
res.json(result.rows);
});
});

app.get("/products", (req, res) => {
db.query(
"select p.product_name, p_a.unit_price,s.supplier_name from products p join product_availability p_a on (p.id = p_a.prod_id) join suppliers s on (s.id = p_a.supp_id)",
(err, result) => {
res.json(result.rows);
}
);
});

app.listen(8080, function () {
console.log("Server is listening on port 8080.");
});
12 changes: 12 additions & 0 deletions node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions node_modules/.bin/mime.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions node_modules/.bin/mime.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading