Upload
brianna-powers
View
236
Download
0
Embed Size (px)
Citation preview
Structured Query Language
Basic Queries in SQL
SQL has one basic statement for retrieving information from a database
The 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
Basic SELECT Statement
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;
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;
ARITHMETIC EXPRESSIONS
Arithmetic operators such as +, -, *, / may be used in SQL statements.ex.SELECT fname, salary+100
FROM employee;
Duplicate Rows
The default display of queries is all rows, including duplicate rows
To eliminate dublicate rows we use DISTINCT keyword in select clause
Duplicate rows
Example : Select distinct salary
from employee
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
FROM employee;Or: SELECT Lname “Employee Record”
FROM employee;
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.
Example
Select SSN, Fname || ' ' || Minit || ' ' || Lname From Employee;
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
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 columnsFrom tableWhere certain_condition;
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, dnoFrom employeeWhere dno = 4;
Comparison Operators =, < , <=,>, >= and <> NOT AND OR
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
Retrieve the birthdate and address of the employee whose name is ‘John B. Smith’
SELECT BDATE,ADDRESSFROM EMPLOYEEWHERE FNAME=‘John’ AND MINIT=‘B’ AND LNAME=‘Smith’;
Retrieve the name(s)of the employee(s) whose salary not equal 38000
SELECT fname,lnameFROM EMPLOYEE
WHERE salary <> 38000;
Retrieve all employees in department 5 whose salary is between $30,000 and $40,000.
SELECT *
FROM EMPLOYEEWHERE (SALARY BETWEEN 30000 AND 40000) AND
DNO = 5;
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 employeeWhere dno=1 OR dno = 2 OR dno = 4;
The above query can be rewritten using IN operator asSelect *From employeeWhere DNO IN (1, 3, 4);
Retrieve the social security numbers of all employees who work on project number 1, 2, or 3.
SELECT DISTINCT ESSNFROM WORKS_ONWHERE PNO IN (1, 2, 3)
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.
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.
Retrieve all employees whose address is Huston, Texas
SELECT FNAME, LNAMEFROM EMPLOYEEWHERE ADDRESS LIKE '%Houston,TX%';
Retrieve all employees who were born during the 1950s.
SELECT FNAME, LNAME
FROM EMPLOYEE WHERE BDATE LIKE ’_ _ _ _ _ _ _ _ _5_';
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, LNAMEFROM EMPLOYEEWHERE SUPERSSN IS NULL
Note: If a join condition is specified, tuples with NULL values for the join attributes are not included in the result