Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured Query Language

Similar presentations


Presentation on theme: "Structured Query Language"— Presentation transcript:

1 Structured Query Language

2 Basic Queries in SQL SQL has one basic statement for retrieving information from a database The Select Statement

3 Basic SELECT Statement
In its simplest form, a SELECT statement must include the following: A SELECT clause, which specifies the columns to be displayed A FROM clause, which specifies the table containing the columns listed in the SELECT clause

4 Selecting All Columns, All Rows
You can display all columns of data in a table by following the SELECT keyword with an asterisk (*). Example: Select * from department;

5 Selecting Specific Columns, All Rows
You can use the SELECT statement to display specific columns of the table by specifying the column names, separated by commas. Example: Select fname , lname from employee;

6 ARITHMETIC EXPRESSIONS
Arithmetic operators such as +, -, *, / may be used in SQL statements. ex. SELECT fname, salary+100 FROM employee;

7 Duplicate Rows The default display of queries is all rows, including duplicate rows To eliminate dublicate rows we use DISTINCT keyword in select clause

8 Duplicate rows Example : Select distinct salary from employee

9 Column Alias Column aliases exist to help organizing output.
The syntax: SELECT column AS column_alias FROM table Ex. How to use alias SELECT Lname As employee FROM employee; Or: SELECT Lname employee Or: SELECT Lname “Employee Record”

10 Concatenation and Literals
For string data types, the concatenate operator || can be used in a query to append two string values. Literals refers to a fixed data value such as string, number or date. It will be viewed one time in each row.

11 Example Select SSN, Fname || ' ' || Minit ||
' ' || Lname From Employee;

12 Ordering of Query Results
SQL allows the user to order the rows in the result of a query by the values of one or more attributes, using the ORDER BY clause. The default order is in ascending order of values. We can specify the keyword DESC if we want to see the result in a descending order of values. The keyword ASC can be used to specify ascending order explicitly. if we want descending order on DNAME and ascending order on LNAME, FNAME the order by clause will be: ORDER BY DNAME DESC, LNAME ASC, FNAME ASC

13 Limiting Row Selection
The queries we wrote so far display all the rows from a table. When you select information from the database sometimes it is desirable to limit the rows that you select. For example if there were 5000 rows of data in the employee table and you wanted to look at the details of only one employee then you would not want to have to select all 5000 rows. You would only want to retrieve the details of the employee you were interested in. You can reduce the number of rows selected by use of the where clause. The where clause is part of the select statement and is optional. The where clause limits the number of rows retrieved according to some criteria The where clause, if used, must follow the FROM clause. It contains a condition, which rows must meet in order to be displayed. Select columns From table Where certain_condition;

14 Example For example if you want to display a list of all those employees who are working in department 4, the SQL statement will be Select ssn, fname, dno From employee Where dno = 4;

15 Comparison Operators =, < , <=,>, >= and <> NOT AND
AND and OR join two or more conditions in a WHERE clause. You can also combine AND and OR. IN (value1,value2,..) The IN operator may be used if you know the exact value you want to return for at least one of the columns. BETWEEN lowest_value AND highest_value To select range of data. LIKE % zero or more _ one character

16 Retrieve the birthdate and address of the employee whose name is ‘John B. Smith’
SELECT BDATE,ADDRESS FROM EMPLOYEE WHERE FNAME=‘John’ AND MINIT=‘B’ AND LNAME=‘Smith’;

17 Retrieve the name(s)of the employee(s) whose salary not equal 38000
SELECT fname,lname FROM EMPLOYEE WHERE salary <> 38000;

18 Retrieve all employees in department 5 whose salary is between $30,000 and $40,000.
SELECT * FROM EMPLOYEE WHERE (SALARY BETWEEN AND 40000) AND DNO = 5;

19 Selecting Rows from a Set of Values Using IN Operator
The IN operator tests for values in a specified list. For example, if you want to list employees who are working in either dno 1, or dno 2 or dno 4, then you may use the following SQL statement Select * From employee Where dno=1 OR dno = 2 OR dno = 4; The above query can be rewritten using IN operator as Select * Where DNO IN (1, 3, 4);

20 Retrieve the social security numbers of all employees who work on project number 1, 2, or 3.
SELECT DISTINCT ESSN FROM WORKS_ON WHERE PNO IN (1, 2, 3)

21 Substring Comparison SQL allows comparison conditions on only parts of a character string , using LIKE comparison operator. Partial strings are specified by using two reserved character: % : replaces an arbitrary number of characters. _ : replaces a single character.

22 Example of substring comparisons
'ABC%': All strings that start with 'ABC'. For example, 'ABCD' and 'ABCABC' would both satisfy the condition. '%XYZ': All strings that end with 'XYZ'. For example, 'WXYZ' and 'ZZXYZ' would both satisfy the condition. '%AN%': All strings that contain the pattern 'AN' anywhere. For example, 'LOS ANGELES' and 'SAN FRANCISCO' would both satisfy the condition. ‘C _ _‘ : Any three characters long string that starts with C . For example , ‘Car’ and ‘Cat’ would both satisfy the condition.

23 Retrieve all employees whose address is Huston, Texas
SELECT FNAME, LNAME FROM EMPLOYEE WHERE ADDRESS LIKE '%Houston,TX%';

24 Retrieve all employees who were born during the 1950s.
SELECT FNAME, LNAME FROM EMPLOYEE WHERE BDATE LIKE ’_ _ _ _ _ _ _ _ _5_';

25 NULLS IN SQL QUERIES SQL allows queries that check if a value is NULL (missing or undefined or not applicable) SQL uses IS or IS NOT to compare NULLs because it considers each NULL value distinct from other NULL values, so equality comparison is not appropriate . Retrieve the names of all employees who do not have supervisors. SELECT FNAME, LNAME FROM EMPLOYEE WHERE SUPERSSN IS NULL Note: If a join condition is specified, tuples with NULL values for the join attributes are not included in the result

26


Download ppt "Structured Query Language"

Similar presentations


Ads by Google