Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL: Single Table Queries SELECT FROM WHERE ORDER D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 1.

Similar presentations


Presentation on theme: "SQL: Single Table Queries SELECT FROM WHERE ORDER D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 1."— Presentation transcript:

1 SQL: Single Table Queries SELECT FROM WHERE ORDER D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 1

2 SELECT: syntax SELECT [ DISTINCT | ALL ]{ * | column_list } FROM [owner.]table_name [alias] [,[owner.]table_name [alias]]... [WHERE condition] [GROUP BY column_list] [HAVING condition] [ORDER BY column_list]; Can accomplish the three relational operations: selection, projection, and join. The order of the clauses in the SELECT statement cannot be changed. The only required clauses are SELECT and FROM. D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 2

3 SELECT [ DISTINCT | ALL ]{ * | column_list } SELECTcarries out the relational operation of projection: select certain columns. column_listone/more column_names from the tables/views listed in the FROM clause. *All columns. The order of fields is the same as the order given in CREATE TABLE. In some dialects it is possible to place table_name.* ALLallows duplicated rows to appear in the result DISTINCTdoes not allow appearing of duplicate rows D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 3

4 SELECT [ DISTINCT | ALL ]{ * | column_list } Expressions affect every row of the result table literal constants: SELECT Item_id, descript, ‘available: ‘, on_hand FROM stock; arithmetic: +, -, *, / SELECT Item_id, ‘Value: ’, price*on_hand FROM stock; Use qualifier: first - to be sure about the result and second it is easier to read SELECT Item_id, price*on_hand AS ‘Value’ FROM stock; non aggregate functions: sqrt, sin, cos, etc D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 4

5 SELECT [ DISTINCT | ALL ]{ * | column_list } Aggregate functions: SUM, AVG, COUNT, MAX, MIN takes entire column(s) and produce a single value; predefined, affected by GROUP BY DISTINCTaffects SUM, AVG, MAX, MIN; not allowed in COUNT(*), but COUNT(DISTINCT column_name) EXAMPLE: SELECT DISTINCT cust_id, item_id FROM orders; EXAMPLE: SELECT COUNT(DISTINCT cust_id) FROM orders; D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 5

6 The FROM clause FROM Must immediately follow the required SELECT clause. Lists one or more tables and/or views. Defines the required join on those tables. D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 6

7 fancy_fruits DB (MySQL Implementation) D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 7

8 Illustrative examples (1) cust_idcust_nameregionphone AAAALLICENE(555)111-1111 BBBBILLW(555)222-2222 CCCCAITLINNE(555)333-3333 DDDCOLINS(555)444-4444 EEEELIZABETHW(555)555-5555 LLLLAURANE(555)666-6666 D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 8 fancy_fruits.customers SELECT region, cust_name FROM customers; SELECT cust_name, phone FROM customers;

9 Illustrative examples (2) order_nocust_iditem_idquantity 001AAAI0110 002BBBI0220 003AAAI0330 004CCCI0140 005BBBI0550 006AAAI0460 007AAAI0370 008EEEI0720 009CCCI0140 010BBBI0560 D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 9 SELECT item_id, quantity FROM orders; SELECT cust_id, item_id, quantity FROM orders; fancy_fruits.orders

10 The WHERE clause WHEREControls, which rows are retrieved into the result: serve as filter and defines the search conditions simple comparisons =, <>, >, =, <= compound condition AND, OR, NOT special SQL predicate BETWEEN, IN, LIKE, NULL comparison with the result of a sub-query additional predicates for use with a sub-query: ANY, ALL, EXISTS D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 10

11 Illustrative examples (3) cust_idcust_nameregionphone AAAALLICENE(555)111-1111 BBBBILLW(555)222-2222 CCCCAITLINNE(555)333-3333 DDDCOLINS(555)444-4444 EEEELIZABETHW(555)555-5555 LLLLAURANE(555)666-6666 D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 11 fancy_fruits.customers SELECT * FROM customers WHERE region = ‘NE’; SELECT * FROM customers WHERE region = ‘ne’;

12 Illustrative examples (4) cust_idcust_nameregionphone AAAALLICENE(555)111-1111 BBBBILLW(555)222-2222 CCCCAITLINNE(555)333-3333 DDDCOLINS(555)444-4444 EEEELIZABETHW(555)555-5555 LLLLAURANE(555)666-6666 D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 12 fancy_fruits.customers SELECT cust_name FROM customers WHERE length(cust_name) > 4;

13 Illustrative examples (5) cust_idcust_nameregionphone AAAALLICENE(555)111-1111 BBBBILLW(555)222-2222 CCCCAITLINNE(555)333-3333 DDDCOLINS(555)444-4444 EEEELIZABETHW(555)555-5555 LLLLAURANE(555)666-6666 D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 13 fancy_fruits.customers SELECT cust_name, phone FROM customers WHERE cust_name=‘CAITLIN’ OR cust_name=‘LAURA’;

14 Illustrative examples (6) cust_idcust_nameregionphone AAAALLICENE(555)111-1111 BBBBILLW(555)222-2222 CCCCAITLINNE(555)333-3333 DDDCOLINS(555)444-4444 EEEELIZABETHW(555)555-5555 LLLLAURANE(555)666-6666 D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 14 fancy_fruits.customers SELECT * FROM customers WHERE phone IS NULL; phone = NULL | <> NULL

15 Illustrative examples (7) cust_idcust_nameregionphone AAAALLICENE(555)111-1111 BBBBILLW(555)222-2222 CCCCAITLINNE(555)333-3333 DDDCOLINS(555)444-4444 EEEELIZABETHW(555)555-5555 LLLLAURANE(555)666-6666 D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 15 fancy_fruits.customers SELECT * FROM customers WHERE cust_name LIKE ‘Alice’; ‘%li%’ ‘%’ is a wildcard – zero or more characters ‘A_ _ c%’‘_’ one single character

16 Illustrative examples (8) order_nocust_iditem_idquantity 001AAAI0110 002BBBI0220 003AAAI0330 004CCCI0140 005BBBI0550 006AAAI0460 007AAAI0370 008EEEI0720 009CCCI0140 010BBBI0560 D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 16 SELECT item_id, quantity FROM orders WHERE quantity > AVG(quantity) ; fancy_fruits.orders SELECT item_id, quantity FROM orders WHERE quantity > 50 ;

17 Illustrative examples (9) cust_idcust_nameregionphone AAAALLICENE(555)111-1111 BBBBILLW(555)222-2222 CCCCAITLINNE(555)333-3333 DDDCOLINS(555)444-4444 EEEELIZABETHW(555)555-5555 LLLLAURANE(555)666-6666 D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 17 fancy_fruits.customers SELECT * FROM orders WHERE cust_id IN ( ‘CCC’, ‘EEE’, ’LLL’); a IN (x, y, z) is equivalent of (a = x) OR (a = y) OR (a = z)

18 Illustrative examples (10) cust_idcust_nameregionphone AAAALLICENE(555)111-1111 BBBBILLW(555)222-2222 CCCCAITLINNE(555)333-3333 DDDCOLINS(555)444-4444 EEEELIZABETHW(555)555-5555 LLLLAURANE(555)666-6666 D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 18 fancy_fruits.customers SELECT * FROM orders WHERE quantity BETWEEN 20 AND 40; a BETWEEN x AND y is equivalent of (a >= x) AND (a <= y)

19 The ORDER BY clause Order according to a list of column_names from the list of column_names specified in SELECT. Column_numbers (according to the order of fields as they appeared in the SELECT clause) could be used also (useful when specified an expression without identifying it with AS clause). ASC (ascending) is the default option; DESC (descending) must be specified. When more than one order keys are used: the first one specifies the general order (major key), the second one (minor key) - the order into a group with the equal values of the major key, etc. D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 19

20 Illustrative examples (11) cust_idcust_nameregionphone AAAALLICENE(555)111-1111 BBBBILLW(555)222-2222 CCCCAITLINNE(555)333-3333 DDDCOLINS(555)444-4444 EEEELIZABETHW(555)555-5555 LLLLAURANE(555)666-6666 D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 20 fancy_fruits.customers SELECT * FROM customers ORDER BY region, cust_name; SELECT * FROM customers ORDER BY 3, 1 DESC;

21 quack_consulting DB (MySQL Implementation) D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 21

22 Quack Consulting DB Examples (1) client_idclient_namecityregionphone A001AliceBolcoS111-1111 B002BillTranforW222-2222 C003CaitlinCalpasNE333-3333 D004ColinTranforW444-4444 L005LauraCalpasNE555-5555 D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 22 quack_consulting.Clients 1.List all clients from region W 2.List all clients, which names started with ‘C’ 3.What the following query will print? SELECT name, phone FROM clients WHERE phone LIKE ‘_ _ 4%’;

23 Quack Consulting DB Examples (2) skill_idskill_descrbilling_rate ANAnalysis60.00 DEDesign70.00 PRProgram50.00 CPConfig70.00 DDDatabase80.00 D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 23 quack_consulting.Specialty 1.Show all specialties with current billing rate and the billing rate increased with 10%. 2.Find half the sum of the minimum billing rate plus the maximum billing rate. 3.Find all specialties with current billing rates that are lower than the average billing rate.

24 Quack Consulting DB Examples (3) project_idconsultantitemcost P01URIConnectors200.00 P01URIBoards900.00 P01SUECalculator20.00 P03RAYKeyboard100.00 P05TEDCharts5.00 P02TEDModem200.00 D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 24 quack_consulting.Purchases 1.Find the total cost of all purchases, associated with project P01. 2.Find the minimum, maximum, and average cost.

25 Quack Consulting DB Examples (4) project_idconsultantdatehours P01SUE2013-06-265.0 P01SUE2013-06-283.0 P03TED2013-07-136.0 P01URI2013-06-304.0 P03TED2013-07-157.0 P05TED2013-06-221.0 P03RAY2013-07-154.0 P05TED2013-06-243.0 D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 25 quack_consulting.Time 2.List all time entries for 4 or more hours logged by Sue or Ted between 6/25/2013 and 7/14/2013. 3.List all time entries except those for which the consultant is Sue or the date falls during July 2013. 1.Find all times logged for projects P01 and P03 by Ted and Sue.

26 Questions? Attention ! Next class Quiz 4: Single Table Queries D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 26


Download ppt "SQL: Single Table Queries SELECT FROM WHERE ORDER D. Christozov / G.Tuparov INF 280 Database Systems: Single Table Queries 1."

Similar presentations


Ads by Google