Browse Source

Niet alle oefeningen kan ik maken

master
LAPTOP-92DVNLKB\Tom 2 years ago
parent
commit
266023f100

+ 22
- 6
How-to-code-subqueries.sql View File

@@ -1,19 +1,35 @@
-- Exercise 1

SELECT DISTINCT category_name
FROM categories
WHERE category_id IN
(SELECT category_id
FROM products)
ORDER BY category_name

-- Exercise 2

SELECT product_name, list_price
FROM products
WHERE list_price >
(SELECT AVG(list_price)
FROM products)
ORDER BY list_price DESC;

-- Exercise 3

SELECT category_name
FROM categories
WHERE NOT EXISTS
(SELECT *
FROM products
WHERE category_id = categories.category_id);

-- Exercise 4

???

-- Exercise 4 Part 2
???

-- Exercise 5
???

-- Exercise 6
???

+ 23
- 6
How-to-code-summary-queries.sql View File

@@ -1,19 +1,36 @@
-- Exercise 1

SELECT COUNT(*) AS orders, SUM(tax_amount) AS tax
FROM orders;

-- Exercise 2

SELECT cat.category_name, list_price, COUNT(*) AS product_count
FROM categories cat
JOIN products pro ON cat.category_id = pro.category_id
GROUP BY category_name
ORDER BY product_count DESC
LIMIT 1;

-- Exercise 3

SELECT email_address, SUM(item_price) * quantity AS sum_price, SUM(discount_amount) * quantity AS sum_discount
FROM customers cus
JOIN orders ord ON ord.customer_id = cus.customer_id
JOIN order_items oi ON oi.order_id = ord.order_id
GROUP BY email_address
ORDER BY sum_price DESC;

-- Exercise 4

SELECT email_address, COUNT(*) AS order_count, (item_price - discount_amount) * quantity AS total_amount
FROM customers cus
JOIN orders ord ON cus.customer_id = ord.customer_id
JOIN order_items oid ON ord.customer_id = oid.order_id
GROUP BY email_address HAVING order_count > 1
ORDER BY total_amount DESC;

-- Exercise 5

???

-- Exercise 6
???

-- Exercise 7
???

+ 20
- 10
How-to-insert-update-and-delete-data.sql View File

@@ -1,30 +1,40 @@
-- Exercise 1

INSERT INTO categories (category_name)
VALUES ('Brass')

-- Exercise 2
???

-- Exercise 3

DELETE FROM categories
WHERE category_id = 5

-- Exercise 4

INSERT INTO products(category_id, product_code, product_name, description, list_price, discount_percent, date_added)
VALUES (4, 'dgx_640', 'Yamaha DGX 640 88-Key Digital Piano', 'Long description to come.', 799.99, 0, now())

-- Exercise 5

UPDATE products
SET discount_percent = 35
WHERE product_id = 11

-- Exercise 6
???

-- Exercise 7

INSERT INTO customers(email_address, password, first_name, last_name)
VALUES ('rick@raven.com', ' ', 'Rick', 'Raven')

-- Exercise 8

UPDATE customers
SET password = "secret"
WHERE email_address = 'rick@raven.com'

-- Exercise 9

UPDATE customers
SET password = "reset"
LIMIT 100

-- Exercise 10
///


+ 34
- 7
How-to-retrieve-data-from-a-single-table.sql View File

@@ -1,22 +1,48 @@
-- Exercise 1

SELECT product_name, list_price, date_added, discount_percent
FROM products
ORDER BY list_price DESC;

-- Exercise 2

SELECT first_name, last_name,
CONCAT(first_name, ', ', last_name) AS last_name
FROM customers
WHERE last_name > 'M'
ORDER BY last_name ASC;

-- Exercise 3

SELECT product_name, list_price, date_added
FROM products
WHERE list_price > 500 AND list_price < 2000
ORDER BY date_added DESC;

-- Exercise 4

SELECT product_name, list_price, discount_percent,
ROUND(((list_price * discount_percent) / 100), 2) AS discount_amount,
ROUND(((list_price - (list_price * discount_percent) / 100)), 2) AS discount_price
FROM products
ORDER BY discount_price DESC
LIMIT 5;

-- Exercise 5

SELECT item_id, item_price, discount_amount, quantity,
item_price * quantity AS price_total,
discount_amount * quantity AS discount_total,
(item_price - discount_amount) * quantity AS item_total
FROM order_items
HAVING item_total > 500
ORDER BY item_total DESC;

-- Exercise 6

SELECT order_id, order_date, ship_date
FROM orders
WHERE ship_date IS NULL;

-- Exercise 7
SELECT NOW() AS today_unformatted,
DATE_FORMAT(NOW(), '%e-%b-%Y') AS today_formatted;


-- Exercise 8
SELECT 100 AS price, .07 AS tax_rate,
100 * .07 AS tax_amount,
100 + (100 * .07) AS total;

+ 22
- 6
How-to-retrieve-data-from-two-or-more-tables.sql View File

@@ -1,19 +1,35 @@
-- Exercise 1

SELECT category_name, product_name, list_price
FROM categories JOIN products
ORDER BY category_name, list_price ASC;

-- Exercise 2

SELECT first_name, last_name, line1, city, state, zip_code
FROM customers c1 JOIN addresses c2 ON c1.customer_id = c2.customer_id
WHERE email_address = 'allan.sherwood@yahoo.com';

-- Exercise 3

SELECT first_name, last_name, line1, city, state, zip_code
FROM customers c1 JOIN addresses c2 ON c1.customer_id = c2.customer_id
?????

-- Exercise 4

SELECT last_name, first_name, order_date, product_name, item_price, discount_amount, quantity
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
JOIN order_items oi ON o.order_id = oi.order_id
JOIN products p ON oi.product_id = p.product_id
ORDER BY last_name, order_date, product_name;

-- Exercise 5

SELECT DISTINCT p1.product_name, p2.list_price
FROM products p1 JOIN products p2
ON p1.product_id != p2.product_id
AND p1.list_price = p2.list_price
ORDER BY product_name;

-- Exercise 6

???

-- Exercise 7
???

Loading…
Cancel
Save