Structured Query Language (SQL) : Prepared By: Raquel Ofreneo
Structured Query Language (SQL) : Prepared By: Raquel Ofreneo
Structured Query Language (SQL) : Prepared By: Raquel Ofreneo
2
What is SQL?
SQL (Structured Query Language) is a standard interactive programming
language for getting information from and updating to a database.
Statements take the form of a command language that lets you:
SELECT data
INSERT data
UPDATE data
DELETE data
3
Guidelines in writing SQL Statements
SQL statements are not case-sensitive
SQL statements can be on one or more lines
SQL statements are optionally ended with “;”
Keywords cannot be abbreviated or split across lines
Clauses are usually placed on separate lines
Indents are used to enhance readability
Keywords are typically entered in uppercase; all other words such as table
name and columns are entered in lower case
4
Basic SELECT Statement
Use the SELECT statement to retrieve data from one or more tables:
To choose specific columns on the table for display, you specify each column
separated by a comma (,)
6
Choosing Columns
SELECT *
FROM departments;
8
Rows may be limited by:
EQUALS CONDITION
Display rows based on an exact match of values.
SELECT last_name,
salary
FROM employee
WHERE salary = 30000
SELECT employee_id,
last_name
FROM employee
WHERE manager_name = ‘Taylor’
9
Rows may be limited by:
>, < or <> CONDITION
SELECT last_name
FROM employee
WHERE last_name LIKE ‘K*’
12
Rows may be limited by:
LOGICAL CONDITION
AND, OR, NOT
SELECT last_name,
job_id,
FROM employee
WHERE commission_pct IS NOT NULL 14
Sorting Rows
ORDER BY clause
ASC specifies an ascending order
DESC specifies a descending order
SELECT last_name,
salary,
job_id
FROM employee
ORDER BY salary DESC, job_id ASC
• Display the result in descending order by the attribute
salary. If two records have the same attribute value, the
salary sorting criteria is in ascending order according to the
15
Specific row or rows are modified if you specify the WHERE clause
UPDATE employee
SET
All rows inmanager_id = 800 if you omit the WHERE clause
the table are modified
WHERE employee_id = 10
20
Updating Rows in a Table
Values for all the rows in the table are modified if you omit the WHERE
clause
UPDATE employee
SET manager_id = 700
All rows in the table are modified if you omit the WHERE clause
21
Updating Two Columns with a Subquery
Update employee 113’s job and salary to match those of employee 205
UPDATE employee
SET job_id = (SELECT job_id
FROM employee
WHERE employee_id = 205)
salary = (SELECT salary
FROM employee
WHERE employee_id = 205)
WHERE employee_id = 113
22
Basic DELETE Statement
DELETE [FROM] table
[WHERE condition] ;
23
Deleting Rows in a Table
A specific row or specific rows are deleted if you specify the WHERE
clause
All rows in the table are deleted if you omit the WHERE clause
DELETE FROM employee;
24
Deleting Rows Based on Another Table
Use subqueries in DELETE statements to delete rows in a table based on values
from another table
25
Key Points
26
Comments & Questions
27