Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab 2: Retrieving Data from the Database

Similar presentations


Presentation on theme: "Lab 2: Retrieving Data from the Database"— Presentation transcript:

1 Lab 2: Retrieving Data from the Database
CISB224 02A, 02B Semester I, 2009/2010 College of Information Technology, Universiti Tenaga Nasional

2 The SELECT Statement Syntax SELECT select_list FROM table_source [WHERE search_condition] [GROUP BY group_by_expression] [HAVING search_condition] [ORDER BY order_by_expression] A query must be comprised of the SELECT and FROM clauses All other clauses are optional (denoted by [ ]) College of Information Technology, Universiti Tenaga Nasional

3 The SELECT Clause Syntax SELECT [ALL | DISTINCT] { * | {column_name | expression} [[AS] column_alias] } [,…n] This is called the select list! College of Information Technology, Universiti Tenaga Nasional

4 The SELECT Clause – cont.
The select list must be comprised (denoted by {}) of one or more of the following (denoted by [,…n]) * - a “shortcut” that means all columns in the table {column_name|expression} – your choice (denoted by |) of either a column from the table or an expression that evaluates to a value e.g. a string, number, date, etc. College of Information Technology, Universiti Tenaga Nasional

5 The SELECT Clause – cont.
Example 1 SELECT Name, Title FROM emp Display the employees’ names and their job titles. Note! SQL is not case-sensitive Write clauses on separate lines to enhance readability College of Information Technology, Universiti Tenaga Nasional

6 The SELECT Clause – cont.
A column alias is used to: Replace the heading of a table column in the result Provide a heading to a column formed from an expression Use ‘ ’ to enclose a column alias when it is made up of two or more words College of Information Technology, Universiti Tenaga Nasional

7 The SELECT Clause – cont.
Example 2 SELECT Name Employee_Name, Title ‘Job Title’ FROM emp Display the employees’ last names with the heading Employee_Name and their job titles with the heading ‘Job Title’. College of Information Technology, Universiti Tenaga Nasional

8 The SELECT Clause – cont.
The DISTINCT argument is used when you do not want to view duplicate rows in the result ALL is the default argument and need not be specified College of Information Technology, Universiti Tenaga Nasional

9 The SELECT Clause – cont.
Example 3 Display the State where customers are located. SELECT DISTINCT State FROM Cust College of Information Technology, Universiti Tenaga Nasional

10 The SELECT Clause – cont.
Arithmetical expressions in SQL: may be formed from columns of numeric type and/or constants are governed by the normal evaluation rules 2 * (3 + 4) = ? Operation Operator Addition + Subtraction - Multiplication * Division / The addition operator, +, may also be used for string concatenation College of Information Technology, Universiti Tenaga Nasional

11 The SELECT Clause – cont.
Example 4 SELECT Name, BuyingPrice + BuyingPrice * 25 / 100 FROM Product Display the products’ names and their new price as 25% more than the current price College of Information Technology, Universiti Tenaga Nasional

12 The SELECT Clause – cont.
Example 5 SELECT ID + ‘ ’ + Name as ‘Employee Detail’ FROM emp Display the employees’ full name as one column. College of Information Technology, Universiti Tenaga Nasional

13 The ORDER BY Clause Syntax [ORDER BY {order_by_expression [ASC|DESC]} [,…n]] College of Information Technology, Universiti Tenaga Nasional

14 The ORDER BY Clause – cont.
The order_by_expression may be: A column name May or may not be in the select list A column alias Expressions other than the above The position of the expression in the select list College of Information Technology, Universiti Tenaga Nasional

15 The ORDER BY Clause – cont.
ASC On characters - alphabetical order On numbers - increasing order On dates – chronological order DESC – opposite of the above! College of Information Technology, Universiti Tenaga Nasional

16 The ORDER BY Clause – cont.
Example 6 SELECT StartDate, Name, ID FROM emp ORDER BY StartDate DESC Display all employees’ start date, ID and name. Sort the results by start date and name, both in descending order. College of Information Technology, Universiti Tenaga Nasional

17 The ORDER BY Clause – cont.
Example 7 Display all employees’ start date along with ID and name as one column i.e. ‘E60002, Zarina Majid. Sort the results by start date and name, both in descending order. College of Information Technology, Universiti Tenaga Nasional

18 The ORDER BY Clause – cont.
Example 7 – cont. SELECT StartDate, ID + ‘, ’ + Name AS Emp_Detail FROM emp ORDER BY StartDate DESC, 2 DESC ORDER BY StartDate DESC, Emp_Detail DESC ORDER BY StartDate DESC, ID + ‘, ’ + Name DESC College of Information Technology, Universiti Tenaga Nasional

19 The WHERE clause Syntax [WHERE column_name operator expression]
This syntax is simplified for the purpose of today’s (very gentle) introduction to the WHERE clause  College of Information Technology, Universiti Tenaga Nasional

20 The WHERE clause – cont. Comparison Operators = Is equal to <>
Is not equal to < Is less than <= Is less than or equal to > Is greater than >= Is greater than or equal to College of Information Technology, Universiti Tenaga Nasional

21 The WHERE clause – cont. Example 8
SELECT Name, Salary, Dept FROM emp WHERE Dept=‘Admin’ Display the names, salary and department for all employees in Admin department. College of Information Technology, Universiti Tenaga Nasional

22 The WHERE clause – cont. Example 9
SELECT * FROM Inventory WHERE AmountInStock < ReorderPoint Display all products whose amount in stock have fallen below the reorder point. College of Information Technology, Universiti Tenaga Nasional

23 The WHERE clause – cont. Logical Operators BETWEEN
Between two values, inclusive ReorderPointBETWEEN 25 AND 50 IN In a list of values State IN (Johor, Perak, KL) College of Information Technology, Universiti Tenaga Nasional

24 The WHERE clause – cont. Example 10
SELECT Name, Salary, Dept FROM emp WHERE Salary BETWEEN 1200 AND 2000 Display the names of all employees whose salary is between 1200 and 2000 College of Information Technology, Universiti Tenaga Nasional

25 The WHERE clause – cont. Example 11
SELECT State FROM Cust WHERE State IN (Johor, Perak, KL) Display all suppliers who are located in Johor, Perak and KL. College of Information Technology, Universiti Tenaga Nasional


Download ppt "Lab 2: Retrieving Data from the Database"

Similar presentations


Ads by Google