1ST and 2ND Quarter Prog F2F Final Version
1ST and 2ND Quarter Prog F2F Final Version
1ST and 2ND Quarter Prog F2F Final Version
EXAMPLE:
CREATE DATABASE PEARL
CREATE TABLE SECTION (ID INTEGER PRIMARY KEY, FIRST
VARCHAR(10), LAST VARCHAR (10))
Data Definition Language
• ALTER
ALTER TABLE <TABLE NAME>
ADD ATTR DATATYPE
ALTER TABLE <TABLE NAME>
DROP COLUMN ATTR
EXAMPLE:
ALTER TABLE FOODCART ADD SOLD INT
ALTER TABLE FOODCART DROP COLUMN PROFIT
Data Definition Language
• DROP
Has 2 Options:
• CASCADE: Specifies that any foreign key constraint violations
that are caused by dropping the table will cause the
corresponding rows of the related table to be deleted.
• RESTRICT: blocks the deletion of the table of any foreign key
constraint violations would be created.
EXAMPLE:
DROP TABLE <table name> [ RESTRICT|CASCADE]
Data Manipulation Language
• INSERT
-adds new rows to a table
INSERT INTO <TABLE NAME>
VALUES ('value1', 'value2', NULL)
EXAMPLE:
INSERT INTO FOODCART VALUES (’02/26/08', ‘pizza', 70)
Data Manipulation Language
• UPDATE
- modifies one or more attributes
UPDATE <TABLE NAME> SET <ATTR> = <VALUE>
WHERE <SELECTION CONDITION>
EXAMPLE:
UPDATE FOODCART SET SOLD = 349 WHERE DATE = ’02/25/08’
AND FOOD = ‘PIZZA’
Data Manipulation Language
• DELETE
- deletes one or more rows from a table
DELETE FROM <TABLE NAME>
WHERE <CONDITION>
EXAMPLE:
DELETE * FROM FOODCART WHERE FOOD = ‘HOTDOG’
SQL Statements, Operations and Clauses
SQL Statements:
• Select
SQL Operations:
• Join
• Left Join
• Right Join
• Like
SQL Clauses:
• Order By
• Group By
• Having
SQL Statements
• SELECT
SELECT <attribute name> FROM <tables> WHERE <condition>
SELECT * FROM … WHERE
SELECT DISTINCT * FROM … WHERE
EXAMPLE:
SELECT * FROM person WHERE age > 30
SELECT weight FROM person WHERE age > 30
SELECT distinct weight FROM person WHERE age > 30
SQL Operations
• JOIN
- A join can be specified in the FROM clause which list the two input
relations and the WHERE clause which lists the join condition
SQL Operations
• Inner Join = join
EXAMPLE:
SELECT * FROM emp join dept (or FROM emp, dept) on emp.id =
dept.id
• Left Outer Join = left join
EXAMPLE:
SELECT * FROM emp left join dept on emp.id = dept.id
• Right Outer Join = right join
EXAMPLE:
SELECT * FROM emp right join dept on emp.id = dept.id
SQL Operations
• LIKE
% (arbitrary string)
SELECT * FROM emp WHERE ID like ‘%01’;
finds ID that ends with 01, e.g. 1001, 2001, etc.
_ (a single character)
SELECT * FROM emp WHERE ID like ‘_01_’;
finds ID that has the second and third character as 01, e.g. 1010,
1011, 1012, 1013, etc.
SQL Clauses
• ORDER BY
desc (descending order)
SELECT * FROM emp order by state desc
puts state in descending order, e.g. TN, MA, CA
EXAMPLE:
SELECT food, sum(sold) as totalSold FROM FoodCart group by
food
SQL Clauses
• HAVING
EXAMPLE:
SELECT food, sum(sold) as totalSold FROM FoodCart group by food having
sum(sold) > 450
Aggregate Functions
• AGGREGATE FUNCTIONS
-Are used to provide summarization information for SQL
statements, which return a single value.
• COUNT(attr)
• SUM(attr)
• MAX(attr)
• MIN(attr)
• AVG(attr)
Aggregate Functions
• COUNT(attr) -> return # of rows that are not null
Ex: COUNT (distinct food) from FoodCart; -> 2
• SUM(attr) -> return the sum of values in the attr
Ex: SUM(sold) from FoodCart; -> 919
• MAX(attr) -> return the highest value from the attr
Ex: MAX(sold) from FoodCart; -> 500
• MIN(attr) -> return the lowest value from the attr
Ex: MIN(sold) from FoodCart; -> 70
• AVG(attr) -> return the average value from the attr
Ex: AVG(sold) from FoodCart; -> 306.33
• Note: value is rounded to the precision of the datatype
CONTENT
• UNION Operator
• IN Operator
• AND Operator
• OR Operator
• NOT Operator
• NULL Values
• NULL Operator
• Arithmetic Operators
• Subquery
The SQL UNION Operator
• UNION Syntax:
SELECT column_name(s) FROM table1 UNION SELECT
column_name(s) FROM table2;
• UNION ALL Syntax:
The UNION operator selects only distinct values by default. To
allow duplicate values, use UNION ALL:
SELECT column_name(s) FROM table1 UNION ALL SELECT
column_name(s) FROM table2;
The SQL UNION Operator
• UNION
EXAMPLE:
SELECT City FROM Customers UNION SELECT City FROM Suppliers
ORDER BY City;
• UNION ALL
EXAMPLE:
SELECT City FROM Customers UNION ALL SELECT City FROM Suppliers
ORDER BY City;
THE SQL IN Operator
IN Syntax:
SELECT column_name(s) FROM table_name WHERE
column_name IN (value1, value2, ...);
THE SQL IN Operator
• IN
EXAMPLE:
SELECT * FROM Customers WHERE Country IN ('Germany', 'France',
'UK’);
• NOT IN
EXAMPLE:
SELECT * FROM Customers WHERE Country NOT IN ('Germany',
'France', 'UK');
The SQL AND, OR and NOT Operators
The WHERE clause can be combined with AND, OR, and NOT
operators.
• OR Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
• NOT Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
The SQL AND, OR and NOT Operators
• AND
EXAMPLE:
SELECT * FROM Customers WHERE Country='Germany' AND City='Berlin’;
• OR
EXAMPLE:
SELECT * FROM Customers WHERE City='Berlin' OR City='München’;
• NOT
EXAMPLE:
SELECT * FROM Customers WHERE NOT Country='Germany';
Combining AND, OR and NOT
EXAMPLE:
SELECT * FROM Customers WHERE Country='Germany' AND
(City='Berlin' OR City='München’);
EXAMPLE:
SELECT * FROM Customers WHERE NOT Country='Germany' AND
NOT Country='USA';
NULL Value
What is a NULL Value?
• A field with a NULL value is a field with no value.
• If a field in a table is optional, it is possible to insert a new record or update a
record without adding a value to this field. Then, the field will be saved with
a NULL value.
• Note: It is very important to understand that a NULL value is different from
a zero value or a field that contains spaces. A field with a NULL value is one
that has been left blank during record creation!
• IS NULL
EXAMPLE:
SELECT LastName, FirstName, Address FROM Persons WHERE
Address IS NULL;
• IS NOT NULL
EXAMPLE:
SELECT LastName, FirstName, Address FROM Persons WHERE
Address IS NOT NULL;
Arithmetic Operators
+ Addition
- Subtraction
* Multiplication
/ Division
• Addition
EXAMPLE:
SELECT ID, Lastname, Firstname, Salary + 1000 AS ADDED_SALARY FROM
EMPLOYEES
• Subtraction
EXAMPLE:
SELECT Lastname, Branch, Salary - 5000 AS LOWER_SALARY, Commission - 500 AS
LOWER_COMMISSION FROM EMPLOYEES
Arithmetic Operators
• Multiplication
EXAMPLE:
SELECT ID, Firstname, Salary * 12 AS ANNUAL_SALARY FROM
EMPLOYEES
• Division
EXAMPLE:
SELECT ID, Branch, Salary / 5 AS DIVIDED_SALARY, Commission /
5 AS DIVIDED_COMMISSION FROM EMPLOYEES
Subquery Syntax
• SELECT select_list
FROM table_name
WHERE operator
(SELECT select_list FROM table_name)
• The subquery (inner query) executes before the main query
(outer query)
• The result of the subquery is used by the main query
Table
ID Lastname Salary
1 Lim 13000
2 Tan 9000
3 Santos 10000
4 Sy 21000
5 Chu 11000
Subquery Examples
EX:
1. SELECT Lastname, Salary FROM EMPLOYEES WHERE Salary > (SELECT
Salary FROM EMPLOYEES WHERE Lastname = ‘Chu’)
OUTPUT:
Lastname Salary
Lim 13000
Sy 21000
Comparison Operators
Operator Meaning
= Equal to
Tan 9000
Subquery Examples
ID Lastname MIN(Salary)
1 Lim 13000
4 Sy 21000
Multiple-Row Subqueries
Operator Meaning
ALL Returns true if the relation for all elements are true
Subquery Examples
5. SELECT Lastname, Salary FROM EMPLOYEES WHERE Salary < Any
(SELECT Salary FROM EMPLOYEES WHERE ID = 1 OR ID = 4)
OUTPUT:
Lastname Salary
Lim 13000
Tan 9000
Santos 10000
Chu 11000
Subquery Examples
6. SELECT Lastname, Salary FROM EMPLOYEES WHERE Salary < ALL
(SELECT Salary FROM EMPLOYEES WHERE ID = 1 OR ID = 3)
OUTPUT:
Lastname Salary
Tan 9000
CONTENT
• EXISTS Operator
EXISTS Operator
• EXISTS Operator displays the records that exist in the table.
EXAMPLE:
SELECT Lastname, Salary FROM EMPLOYEES WHERE EXISTS (SELECT
Salary FROM EMPLOYEES WHERE Lastname = 'Chu’)
OUTPUT:
Lastname Salary
Lim 13000
Tan 9000
Santos 10000
Sy 21000
Chu 11000