Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
ZEIT2301 Design of Information Systems SQL: SELECT Queries School of Engineering and Information Technology UNSW@ADFA Dr Kathryn Merrick Topic 11: SQL SELECT Queries Objectives To introduce the basic notation for writing SELECT queries in SQL including: Query structure Wildcards in SELECT Mathematical operators in SELECT Set membership Queries in SQL The most common operation on a database is to query the data in it For this reason our main interest will be in SQL queries There is only one command in SQL for querying data - the SELECT command SQL Select Syntax The basic syntax of the SQL Select command is SELECT attribute(s) FROM table(s) WHERE selection condition(s); Note: SQL reserved words are not case sensitive SQL Select Parameters A table in a relational database consists of data in rows (records, tuples) and columns (attributes, fields). The SELECT clause, with its list of attributes, determines which columns will be displayed. The WHERE clause in SQL determines which rows will be retrieved from the table. Worked Example • Given the table: student (ID, Name, DOB) ID 833801 930047 957719 834027 927440 940456 954065 950145 954007 Name Smith Williams Marks Jones Randall Fotheringale Morton King Baldwin DOB 18/03/79 02/05/83 24/09/83 11/06/78 16/06/88 21/12/81 15/10/82 02/01/84 08/04/86 Worked Example (cont.) Consider the following query: SELECT Name, DOB FROM student WHERE ID = 957719; Note: quotes not required for numeric values -- Attributes -- Table -- Rows Worked Example (cont.) SELECT Name, DOB FROM Student WHERE ID = 957719 Table: student ID 833801 930047 957719 834027 927440 940456 954065 950145 954007 Name Smith Williams Marks Jones Randall Fotheringale Morton King Baldwin DOB 18/03/79 02/05/83 24/09/83 11/06/78 16/06/88 21/12/81 15/10/82 02/01/84 08/04/86 Another Example Given the table definition: student (ID, Name, DOB, Address, Gender) the query: SELECT ID, Name FROM student WHERE DOB > ‘18-Sep-1985’; Note 1: the date format varies in different databases will fetch from the database, the ID and name of all students born after (“>”) 18-Sep-1985 (18/9/1985). Note 2: quotes required for nonnumeric values Another Example (cont) SELECT ID, Name FROM Student WHERE DOB > ‘18-Sep-1985’ Table: student ID 833801 930047 957719 834027 927440 940456 954065 950145 954007 Name Smith Williams Marks Jones Randall Fotheringale Morton King Baldwin DOB 18/03/79 02/05/83 24/09/83 11/06/78 16/06/88 21/12/81 15/10/82 02/01/84 08/04/86 In Class Exercise 1 Given the table definition: student (ID, Name, DOB, Address, Gender) Write the query: that will fetch from the database the name and address of all students born after (>) 20-Sep-1987 Solution: SELECT Name, Address FROM student WHERE DOB > ‘20-Sep-1987’; Select All Attributes If all attributes of the target table are required then, rather than write them all out explicitly, a shorthand is available (“*”): SELECT * FROM student WHERE name = 'Jones'; If all attributes and all rows are required, the FROM clause can be omitted: SELECT * FROM player; Operations in the WHERE clause The basic operations that are allowed in the WHERE clause are: mathematical operations between appropriate operands: +, -, *, / relational comparisons: =, >, <, >=, <=, != boolean (logical) connectives: and, or, not Note: Use brackets to define order of operations , if necessary. Operator Example Example: Use of a boolean operator (AND, OR, NOT): SELECT ID, Name, Address FROM student WHERE DOB > ‘18-Sep-1975’ AND Gender = ‘M’; In Class Exercise 2 Given the table definition player (ID, Name, DOB, Address, Gender) Note: Gender is either > ‘F’ – for female > ‘M’ – for male Write the query: that will fetch from the database the name, address and DOB of all female players born during 1985 Solution: SELECT Name, Address, DOB FROM player WHERE Gender = ‘F’ AND DOB >= '1-Jan-1985' AND DOB <= 31-Dec-1985’; In Class Exercise 3 Given the table definition: stock-item (stock-no, description, department-no, qty-on-hand, buying-price, selling-price, supplier-no) Write the query: that lists the stock-numbers for items whose potential profit is over $500. Write the query: Hint: Potential profit on an item is: selling-price – buying-price Solution SELECT stock-no FROM stock-item WHERE (selling-price – buying-price) > 500; Note: formatting characters, such as “$” not used in query In Class Exercise 4 Given the table definition: stock-item (stock-no, description, department-no, qty-on-hand, buying-price, selling-price, supplier-no) Write the query: that lists, for each stock-item, the stock-number and value of stock on hand (using buying-price as item value) Hint: SELECT clause can have a calculation in place of a column name. buying-price gives the item value; qty-on-hand tells you how many that item you currently have Exercise 4 – Solutions SELECT stock-no, (buying-price * qty-on-hand) FROM stock-item; or SELECT stock-no, (buying-price * qty-on-hand) AS stockValue FROM stock-item; Note: In the second solution the computation is ALIASED using the AS keyword Other Operators Other operators that can be used in the WHERE clause include: LIKE - used for wildcard string searches IS NULL - used to check for null values BETWEEN - used to define range limits IN - used to test for membership of a set of values Wildcards, using LIKE Wildcard characters may also be used: “%” matches any string (zero or more characters) “_” characters, matches exactly one character Example: SELECT Number, Name Note: MS Access uses * as the wildcard FROM student WHERE Name LIKE ‘Smith%’; Matches all names that start with ‘Smith’. Note: ‘like’ for pattern matching, not “=“ IS NULL Given the table definition: stock-item (stock-no, description, department-no, qty-on-hand, buying-price, selling-price, supplier-no) SELECT stock-no FROM stock-item WHERE supplier-no IS NULL; Lists the stock-numbers for rows where the supplier-no is unknown BETWEEN Given the table definition: player (ID, Name, DOB, Address, Height) Which players are between 180cm and 190cm tall? Solution: SELECT Name, Height FROM player WHERE Height BETWEEN 180 and 190; Note: use the numeric value only (ie 180) not 180cms. 22 Set Membership (IN) Given the table definition: player (ID, Name, DOB, Address, Sport) Which players play racquet sports? Solution: SELECT Name FROM player WHERE Sport IN (‘Badminton’, ‘Squash’, ‘Tennis’) ; In Class Exercise 5 Given the table definition: player (ID, Name, DOB, Grade, Gender) Write the query: that will display all details of players in the 1st, 2nd or 3rd grade teams (More than one solution is valid) Exercise 5 - Solutions Solution 1: Note: Can’t use: “grade = 1 or 2”, Must use: “grade = 1 or grade = 2” SELECT * FROM player WHERE grade = 1 OR grade = 2 OR grade = 3; Solution 2: Note: “between” is inclusive in SQL SELECT * FROM player WHERE grade between 1 and 3; Solution 3: SELECT * FROM player WHERE grade IN (1, 2, 3); Ordering Ordering the result of a query is simple - the attribute(s) and direction (i.e. ASC, DESC) in which the ordering is to be done is specified after the (optional) ORDER BY clause (default ASC) Example: SELECT Number, Name, DOB FROM student WHERE DOB > ‘18-Sep-1975’ ORDER BY DOB, Name DESC; Eliminating Duplicates By default most SQL implementations will return every individual row satisfying the query conditions - irrespective of whether there might be duplicates in the output Often this is not a problem, but sometimes it can be inconvenient In SQL duplicates can be eliminated by use of the DISTINCT keyword SQL Select Distinct Example • Given the table: student (ID, Name, DOB) ID 833801 930047 957719 834027 927440 940456 954065 950145 954007 Name Smith Williams Marks Jones Randall Fotheringale Morton King Baldwin Degree BA BSc MSc BA BEng Msc BSc BSc BA SQL Select With Duplicates SELECT Degree FROM Student Table: student ID 833801 930047 957719 834027 927440 940456 954065 950145 954007 Name Degree Smith BA Williams BSc Marks MSc Jones BA Randall BEng Fotheringale Msc Morton BSc King BSc Baldwin BA SQL Select Without Duplicates SELECT DISTINCT Degree FROM student; Produces: Degree BA BSc MSc BEng SQL Select Summary SELECT column1, column2, … FROM table1 WHERE X = ……..; -- Attributes -- Table -- Rows Some handy reserved words: ORDER BY DISTINCT LIKE IS NULL AND OR BETWEEN IN