Exercises - Part 1
Exercise 1
Create a view that meets the following three conditions (the view name is ViewPractice5_1). Using the product table as the reference table, it is assumed that the table contains 8 rows of data in the initial state.
- Condition 1: the sales unit price is greater than or equal to 1000 yen.
- Condition 2: the registration date is September 20, 2009.
- Condition 3: it includes three columns: commodity name, sales unit price and registration date.
The result of executing the SELECT statement on the view is as follows.
SELECT * FROM ViewPractice5_1;
results of enforcement
product_name | sale_price | regist_date --------------+------------+------------ T shirt | 1000 | 2009-09-20 kitchen knife | 3000 | 2009-09-20
Exercise Code:
CREATE VIEW ViewPractices5_1(product_name, sale_price, regist_date) AS SELECT product_name, sale_price, regist_date FROM product WHERE sale_price >= 1000 AND regist_date = '2009-09-20';
Exercise 2
Add viewpractice5 to the view created in exercise 1_ What results will be obtained by inserting the following data into 1?
INSERT INTO ViewPractice5_1 VALUES (' knife ', 300, '2009-11-02');
Exercise results:
An error will be reported when inserting data.
Error Code: 1423. Field of view 'shop.viewpractices5_1' underlying table doesn't have a default value
Exercise 3
Write a SELECT statement based on the following results, where sale_price_all is the average selling unit price of all goods.
product_id | product_name | product_type | sale_price | sale_price_all ------------+-------------+--------------+------------+--------------------- 0001 | T shirt | clothes | 1000 | 2097.5000000000000000 0002 | Punch | Office Supplies | 500 | 2097.5000000000000000 0003 | motion T Shirt | clothes | 4000 | 2097.5000000000000000 0004 | kitchen knife | kitchenware | 3000 | 2097.5000000000000000 0005 | pressure cooker | kitchenware | 6800 | 2097.5000000000000000 0006 | Fork | kitchenware | 500 | 2097.5000000000000000 0007 | Dish cleaning board | kitchenware | 880 | 2097.5000000000000000 0008 | ball pen | Office Supplies | 100 | 2097.5000000000000000
Exercise Code:
SELECT product_id, product_name, product_type, sale_price, (SELECT AVG(sale_price) FROM product) AS sale_price_all FROM product;
Exercise 4
Please write an SQL statement according to the conditions in exercise 1 and create a view (named AvgPriceByType) containing the following data.
product_id | product_name | product_type | sale_price | avg_sale_price ------------+-------------+--------------+------------+--------------------- 0001 | T shirt | clothes | 1000 |2500.0000000000000000 0002 | Punch | Office Supplies | 500 | 300.0000000000000000 0003 | motion T Shirt | clothes | 4000 |2500.0000000000000000 0004 | kitchen knife | kitchenware | 3000 |2795.0000000000000000 0005 | pressure cooker | kitchenware | 6800 |2795.0000000000000000 0006 | Fork | kitchenware | 500 |2795.0000000000000000 0007 | Dish cleaning board | kitchenware | 880 |2795.0000000000000000 0008 | ball pen | Office Supplies | 100 | 300.0000000000000000
Exercise Code:
CREATE VIEW AvgPriceByType AS SELECT product_id, product_name, product_type, sale_price, (SELECT AVG(sale_price) FROM product p2 WHERE p2.product_type = p1.product_type GROUP BY product_type) AS avg_sale_price FROM product p1;
Exercises - Part 2
Exercise 5
When an operation or function contains NULL, the result will all become NULL? (judgment question)
Exercise results:
Yes, all become NULL.
Exercise 6
What results can be obtained by executing the following two SELECT statements on the product table used in this chapter?
① The result is returned to the punch, 320 yuan; Wipe the kitchen board, 790 yuan
SELECT product_name, purchase_price FROM product WHERE purchase_price NOT IN (500, 2800, 5000);
② The result is empty
SELECT product_name, purchase_price FROM product WHERE purchase_price NOT IN (500, 2800, 5000, NULL);
Exercise 7
Classify the items in the product table in exercise 6.1 as follows according to the sales_price.
- Low grade goods: the sales unit price is less than 1000 yen (T-shirts, office supplies, forks, kitchen cleaning boards, ball point pens)
- Middle grade goods: the sales unit price is more than 1001 yen and less than 3000 yen (kitchen knife)
- High end goods: the sales unit price is more than 3001 yen (Sports T-shirt, pressure cooker)
Please write a SELECT statement to count the quantity of goods contained in the above commodity categories. The results are as follows.
results of enforcement
low_price | mid_price | high_price ----------+-----------+------------ 5 | 1 | 2
Exercise Code:
SELECT COUNT(CASE WHEN sale_price <= 1000 THEN product_name ELSE NULL END) AS low_price, COUNT(CASE WHEN sale_price > 1000 AND sale_price <= 3000 THEN product_name ELSE NULL END) AS mid_price, COUNT(CASE WHEN sale_price > 3000 THEN product_name ELSE NULL END) AS high_price FROM product;