Presentation is loading. Please wait.

Presentation is loading. Please wait.

Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.

Similar presentations


Presentation on theme: "Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007."— Presentation transcript:

1 Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007

2 2 AGENDA: Retrieving Information from Tables Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions Working with Expressions Book: Mastering SQL by Martin Gruber Sybex (2000) Chapter : 7-9

3 3 Retrieving Information from Tables Using the SELECT Statement Retrieve the information in Salespeople table with SELECT statement: SELECT snum, sname, city, comm FROM Salespeople; SELECT : a keyword that tells the database this statement is a query. Snum, sname, city, comm : a list of the columns from the table that are being selected by the query. Columns that are not listed here, will not be included as the output. FROM Salespeople : a keyword (like SELECT), that must be present in every query, followed by a space and then the name of the table being used as the source of information (Salespeople).

4 4 Retrieving Information from Tables (cont’d) Selecting all columns SELECT * FROM Salespeople; Selecting only certain columns SELECT sname, comm FROM Salespeople; Eliminating Redundant Data – SELECT DISTINCT snum FROM Orders; – DISTINCT applies to all of the columns specified in the SELECT clause. DISTINCT vs. ALL – DISTINCT : eliminates redundant data from the entire output rows. – ALL : opposite of DISTINCT, it retained the duplicate data as the output rows.

5 5 Retrieving Information from Tables (cont’d) Qualified Selection – The WHERE Clause SQL enables you to define criteria to determine which rows are selected for output. Using WHERE clause. If the condition of the defined criteria at the WHERE clause is TRUE then the data will be retained for output, vise versa. Example: SELECT sname, city FROM Salespeople WHERE city = ‘London’;

6 6 Retrieving Information from Tables (cont’d) Using Inequalities in Predicates Equal to = Greater than > Less than < Greater than or equal to >= Less than or equal to <= Not equal to <> Example: SELECT * FROM Customers WHERE rating > 200;

7 7 Retrieving Information from Tables (cont’d) Using Boolean Operators in Predicates NOT SELECT * FROM Customers WHERE NOT city = ‘San Jose’; AND SELECT * FROM Customers WHERE city = ‘London’ AND rating < 200; OR SELECT * FROM Orders WHERE NOT ((odate = ’10/02/2000’ AND snum > 1002) OR amt > 2000.00);

8 8 Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions The IN Operator IN explicitly defines a set in which a given value may or may not be included. Example: – SELECT * FROM Salespeople WHERE city = ‘Barcelona’ OR city = ‘London’; – SELECT * FROM Salespeople WHERE city IN (‘Barcelona’, ’London’);

9 9 Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions (cont’d) The BETWEEN Operator Similar to IN, but it is sensitive to order (first value must be first in alphabetic or numeric order), and followed by beginning value, the keyword AND, and the ending value. Example: – SELECT * FROM Salespeople WHERE comm BETWEEN.12 AND.10; – SELECT * FROM Salespeople WHERE comm >=.12 AND comm <=.10;

10 10 Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions (cont’d) The LIKE Operator It is used with text datatypes To find substrings It searches a text column to see if part of it matches a string To support this, it uses wildcards, special characters that will match anything. Two type of wildcards: – Underscore characters (_) – Percent sign (%)

11 11 Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions (cont’d) Example: – SELECT * FROM Customers WHERE cname LIKE ‘G%’; – SELECT * FROM Salespeople WHERE sname LIKE ‘P_ _ L’; The IS NULL Operator Try to search records in Customers table where the value of city is NULL SELECT * FROM Customers WHERE city IS NULL;

12 12 Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions (cont’d) Using NOT with Special Operators SELECT * FROM Customers WHERE city IS NOT NULL; – It eliminates NULL values from the output. SELECT * FROM Customers WHERE NOT city IS NULL; – It produces output that contain no NULL values in city. SELECT * FROM Salespeople WHERE city NOT IN (‘London’, ‘San Jose’); – It is the same as  WHERE NOT city in (‘London’, ‘San Jose’); Can also be used in : NOT BETWEEN, NOT LIKE.

13 13 Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions (cont’d) Aggregate Functions Aggregate functions produce a single value for an entire group of columns. List of aggregate functions: – COUNT : produces the number of rows or non-NULL column values that the query selected. – SUM : produces the arithmetic sum of all selected values of a given column – AVG : produces the average (mean) of all selected values of a given column – MAX : produces the largest of all selected values of a given column – MIN : produces the smallest of all selected values of a given column

14 14 Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions (cont’d) Using Aggregate Functions: – Are used like column names in the SELECT clause of queries, and take column as arguments. Example: – SELECT SUM(amt) FROM Orders; – SELECT AVG(amt) FROM Orders; Special Attributes of COUNT: – SELECT COUNT(DISTINCT snum) FROM Orders; – SELECT COUNT(*) FROM Customers; Including Duplicates in Aggregate Functions – SELECT COUNT(ALL rating) FROM Customers; – Using ALL with COUNT  It will not count NULL values, and it takes a column name as an argument.

15 15 Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions (cont’d) Aggregate Built on Expression – SELECT AVG(comm * 100) FROM Salespeople; The GROUP Clause: – It allows you to define a subset of the values in a particular column and apply an aggregate function to the subset. – SELECT snum, odate, MAX(amt) FROM Orders GROUP BY snum, odate;

16 16 Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions (cont’d) The HAVING Clause: For example, you want to do this: SELECT snum, odate, MAX(amt) FROM Orders WHERE MAX(amt) > 3000.00 GROUP BY snum, odate; You can not do the above, because you can not use aggregate functions in a WHERE clause (unless using Subquery), because predicates are evaluated in terms of a single row, whereas aggregate functions are evaluated in term of group of rows. Solution  use HAVING.

17 17 Using IN, BETWEEN, LIKE, IS NULL, and Aggregate Functions (cont’d) The correct query : SELECT snum, odate, MAX(amt) FROM Orders GROUP BY snum, odate HAVING MAX(amt) > 3000.00;

18 18 Working with Expressions Using Value Expression in the SELECT Clause To perform simple numeric computations on the data to put it in a form more appropriate to your needs. SQL allows you to place scalar expressions and constants among the selected columns. These expressions can replace columns in the SELECT clause. Naming expression columns in output by using AS. Example: SELECT snum, sname, city, comm * 100 AS percentage FROM Salespeople;

19 19 Working with Expressions (cont’d) Putting text in your query output – SELECT ‘For’, odate, ‘, there are’, COUNT(DISTINCT onum), ‘orders’ FROM Orders GROUP BY odate;

20 20 Working with Expressions (cont’d) More Advanced Uses of Values Expression Datetime and Interval Expressions – Several datetime datatypes: DATE TIME TIMESTAMP – INTERVALs represent periods of time between DATETIME values, for example. One week, one hour, or three days, ten minutes, and four seconds. – Example: INTERVAL 4 years, 11 months  INTERVAL YEAR TO MONTH ‘4/11’ DATE ‘2/4’98’ + INTERVAL YEAR TO MONTH ‘4/11’ DATE ‘2/4’98’ – DATE ‘2/2’98’ INTERVAL DAY ‘2’

21 21 Working with Expressions (cont’d) Datatype Conversion (CAST) Expressions – Convert one datatype to another within SQL – Does not affect the datatype of the data in the database – Only changes the datatype for the purposes of the current SQL statement – Primary reasons to change datatypes are: To make comparisons that would otherwise be invalid To control how a conversion that could be done automatically is actually performed To produce a datatype that can be properly handled in another language, for example in Embedded SQL, or in an interface. – Example  CAST (’12/03/2001’ AS DATE) – Example  SELECT CAST(onum AS CHAR), CAST(amt AS CHAR) FROM Orders;

22 22 Working with Expressions (cont’d) Conditional (CASE) Expressions – There are two types of CASE variations: CASE using value expressions CASE using predicates – Example: SELECT CASE sname WHEN ‘Peel’ THEN ‘Peal’ ELSE sname END FROM Salespeople WHERE snum = 1001; SELECT CASE WHEN sname = ‘Peel’ THEN ‘Peal’ ELSE sname END FROM Salespeople WHERE snum = 1001;

23 23 Working with Expressions (cont’d) Rules apply for any form of CASE: – Can not mix two forms – Either form can take any number of WHEN... THEN clause – An optional ELSE clause may follow all the WHEN... THEN clauses – Entire CASE form is terminated with the keyword END NULLIF : is a simple variation on CASE. It takes two values as arguments. If the two are the same, it produces a NULL; otherwise, it produces the first value of the two. COALESCE : it traverses a list of values and produces the first one that is not NULL. If all are NULL< it produces NULL.

24 24 Working with Expressions (cont’d) Ordering Output by Column Values Tables are unordered sets Use ORDER BY statement Can specify Ascending (ASC) or Descending (DESC)  Default is Ascending (ASC)

25 25 Working with Expressions (cont’d) Ordering by Multiple Columns – SELECT * FROM Orders ORDER BY cnum DESC, amt DESC; Ordering Aggregate Groups – SELECT snum, odate, MAX(amt) FROM Orders GROUP BY snum, odate ORDER BY snum; Ordering Output by Column Number – SELECT sname, comm FROM Salespeople ORDER BY 2 DESC

26 26 End of Retrieving Information Thank you


Download ppt "Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007."

Similar presentations


Ads by Google