Bài giảng Cơ sở dữ liệu – Database EE4253: Chương 4 - SQL nâng cao giới thiệu tới các bạn về IFNULL, NVL, ISNULL; COALESCE; LIMIT; TOP; Subquery; Division in SQL; SQL Quiz và một số nội dung khác.
Cơ sở liệu – Database EE4253 Vũ Hải 2016 International Research Institute MICA, Hanoi University of Science and Technology SQL Nâng cao Resource: http://www.1keydata.com/sql/sql.html IFNULL, NVL, ISNULL • IFNULL(var1,var2): MySQL, • ISNULL(var1,var2): SQL Server • NVL(var1,var2): Oracle COALESCE • COALESCE ("expression 1", "expressions 2", ) • Trả biểu thức khác NULL số tham số đầu vào NULLIF • NULLIF ("expression 1", "expressions 2") – If "expression = expression " THEN return "NULL" ELSE return "expression 1" LIMIT • LIMIT: MySQL • [SQL Statement ] LIMIT [N]; TOP • Microsoft SQL Server • TOP [N] • TOP [M] PERCENT CASE • CASE ("column_name") WHEN "value1"/"condition1" THEN "result1" WHEN "value2"/"condition1" THEN "result2" [ELSE "resultN"] END Subquery • Subquery đơn giản Find the sales of all stores in the West region • Subquery có tương quan Division in SQL • Sailors, Boats, Reserves • Find names of sailors who’ve reserved all boats Sailors S such that … • SELECT S.sname FROM Sailors S WHERE NOT EXISTS (SELECT B.bid FROM Boats B there is no boat B WHERE NOT EXISTS ( that doesn’t have SELECT R.bid FROM Reserves R a Reserves tuple showing S reserved B WHERE R.bid=B.bid AND R.sid=S.sid)) 10 SQL Quiz • 12 Which SQL statement lets you list all stores whose total sales amount is over 5000? • a) SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES GROUP BY STORE_ID HAVING SUM(SALES_AMOUNT) > 5000; b) SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES GROUP BY STORE_ID HAVING SALES_AMOUNT > 5000; c) SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES WHERE SUM(SALES_AMOUNT) > 5000 GROUP BY STORE_ID; d) SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES WHERE SALES_AMOUNT > 5000 GROUP BY 23 STORE_ID; SQL Quiz • 13 Which SQL statement is the correct one to use to find the earliest date STORE_ID 10 had a sales amount greater than 0? • a) SELECT MAX(SALES_DATE) FROM SALES WHERE STORE_ID = 10 AND SALES_AMOUNT > 0; b) SELECT SALES_DATE FROM SALES WHERE STORE_ID = 10 AND SALES_AMOUNT > 0; c) SELECT MIN(SALES_DATE) FROM SALES WHERE STORE_ID = 10 OR SALES_AMOUNT > 0; d) SELECT MIN(SALES_DATE) FROM SALES WHERE STORE_ID = 10 AND SALES_AMOUNT > 0; 24 SQL Quiz • 14 Which SQL statement lets you find the total number of stores in the SALES table? • a) SELECT COUNT(STORE_ID) FROM SALES; b) SELECT COUNT(DISTINCT STORE_ID) FROM SALES; c) SELECT DISTINCT STORE_ID FROM SALES; d) SELECT COUNT(STORE_ID) FROM SALES GROUP BY STORE_ID; 25 SQL Quiz • 15 Which SQL statement allows you to find the total sales amount for Store ID 25 and the total sales amount for Store ID 45? • a) SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES WHERE STORE_ID IN (25,45) GROUP BY STORE_ID; b) SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES GROUP BY STORE_ID HAVING STORE_ID IN (25,45); c) SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES WHERE STORE_ID IN (25,45); d) SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES WHERE STORE_ID = 25 AND STORE_ID = 45 GROUP BY STORE_ID; 26 SQL Quiz • 16 What is the result of the following SQL statement: SELECT COUNT(DISTINCT STUDENT_ID) FROM EXAM_RESULTS; • a) b) c) d) 27 SQL Quiz • 17 What SQL statement we use to find the average exam score for EXAM_ID = 1? • a) SELECT AVG(EXAM_SCORE) FROM EXAM_RESULTS; b) SELECT AVG(EXAM_SCORE) FROM EXAM_RESULTS WHERE EXAM_ID = 1; c) SELECT AVG(EXAM_SCORE) FROM EXAM_RESULTS GROUP BY EXAM_ID; d) SELECT COUNT(EXAM_SCORE) FROM EXAM_RESULTS WHERE EXAM_ID = 1; 28 SQL Quiz • 18 Which SQL statement we use to find out how many students took each exam? • a) SELECT COUNT(DISTINCT Stduetn_ID) FROM EXAM_RESULTS GROUP BY EXAM_ID; b) SELECT EXAM_ID, MAX(STUDENT_ID) FROM EXAM_RESULTS GROUP BY EXAM_ID; c) SELECT EXAM_ID, COUNT(DISTINCT STUDENT_ID) FROM EXAM_RESULTS GROUP BY EXAM_ID; d) SELECT EXAM_ID, MIN(STUDENT_ID) FROM 29 EXAM_RESULTS GROUP BY EXAM_ID; SQL Quiz • 19 What SQL statement we use to print out the record of all students whose last name starts with 'L'? • a) SELECT * FROM EXAM_RESULTS WHERE LAST_NAME LIKE 'L%'; b) SELECT * FROM EXAM_RESULTS WHERE LAST_NAME LIKE 'L'; c) SELECT * FROM EXAM_RESULTS WHERE LAST_NAME = 'L'; d) SELECT * FROM EXAM_RESULTS WHERE LAST_NAME 'L'; 30 SQL Quiz • 20 What is the result of the following SQL statement: SELECT MAX(EXAM_SCORE) FROM EXAM_RESULTS WHERE EXAM_ID = AND FIRST_NAME LIKE '%E%'; • a) 90 b) 85 c) 100 d) 78 31 SQL Quiz • 21 What SQL statement we use to print out the records of all students whose first name or last name ends in 'A'? • a) SELECT * FROM EXAM_RESULTS WHERE FIRST_NAME LIKE '%A' OR LAST_NAME LIKE '%A'; b) SELECT * FROM EXAM_RESULTS WHERE FIRST_NAME LIKE 'A' OR LAST_NAME LIKE 'A'; c) SELECT * FROM EXAM_RESULTS WHERE FIRST_NAME LIKE 'A%' OR LAST_NAME LIKE 'A%'; d) SELECT * FROM EXAM_RESULTS WHERE FIRST_NAME LIKE 32 '%A%' OR LAST_NAME LIKE '%A%'; SQL Quiz • • 22 What SQL statement we use to find the name of all students who scored better than 90 on the second exam (EXAM_ID = 2)? a) SELECT FIRST_NAME, LAST_NAME FROM EXAM_RESULTS WHERE EXAM_ID = OR Exam_SCORE > 90; b) SELECT FIRST_NAME, LAST_NAME FROM EXAM_RESULTS WHERE EXAM_ID = AND Exam_SCORE > 90; c) SELECT FIRST_NAME, LAST_NAME FROM EXAM_RESULTS WHERE Exam_SCORE > 90; d) SELECT FIRST_NAME, LAST_NAME FROM EXAM_RESULTS WHERE EXAM_ID = HAVING Exam_SCORE > 90; 33 SQL Quiz • 23 What SQL statement we use to find the name of all students who scored better than 180 on all the Exams? • a) SELECT FIRST_NAME, LAST_NAME, SUM(EXAM_SCORE) FROM EXAM_RESULTS GROUP BY FIRST_NAME, LAST_NAME; b) SELECT FIRST_NAME, LAST_NAME, SUM(EXAM_SCORE) FROM EXAM_RESULTS HAVING SUM(EXAM_SCORE) > 180; c) SELECT FIRST_NAME, LAST_NAME, SUM(EXAM_SCORE) FROM EXAM_RESULTS GROUP BY FIRST_NAME, LAST_NAME HAVING SUM(EXAM_SCORE) > 180; d) SELECT FIRST_NAME, LAST_NAME, SUM(EXAM_SCORE) FROM 34 EXAM_RESULTS WHERE EXAM_SCORE > 180 GROUP BY FIRST_NAME, LAST_NAME; SQL Quiz • • 24 How many records does the following SQL statement generate? SELECT * FROM EXAM_RESULTS WHERE LAST_NAME LIKE '%N%' AND EXAM_SCORE > 88; a) b) c) d) No Result 35 SQL Quiz • • 25 How many records does the following SQL statement return? SELECT * FROM EXAM_RESULTS WHERE STUDENT_ID 85; a) b) c) d) 36 37 .. .SQL Nâng cao Resource: http://www.1keydata.com /sql/ sql.html IFNULL, NVL, ISNULL • IFNULL(var1,var2): MySQL, • ISNULL(var1,var2): SQL Server • NVL(var1,var2): Oracle... STORE_ID = 45 GROUP BY STORE_ID; 26 SQL Quiz • 16 What is the result of the following SQL statement: SELECT COUNT(DISTINCT STUDENT_ID) FROM EXAM_RESULTS; • a) b) c) d) 27 SQL Quiz • 17 What SQL statement... INTO BOOK_INFORMATION VALUES (20,'1KEYDATA SQL TUTORIAL',15); 16 SQL Quiz • Which SQL statement lets you remove the table BOOK_INFORMATION from the database? • a) DROP BOOK_INFORMATION; b) DELETE