0% found this document useful (0 votes)
22 views14 pages

Chapter 3

The document describes the main SQL statements including SELECT, WHERE, ORDER BY, INSERT, UPDATE, DELETE and JOIN. It explains the syntax and provides examples for each statement. It also includes exercises for readers to practice different SQL queries.

Uploaded by

loivann99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views14 pages

Chapter 3

The document describes the main SQL statements including SELECT, WHERE, ORDER BY, INSERT, UPDATE, DELETE and JOIN. It explains the syntax and provides examples for each statement. It also includes exercises for readers to practice different SQL queries.

Uploaded by

loivann99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

CHAPTER 3

SQL STATEMENT
1. Select statement
- Objectives: to extract data from the database
- Syntax:
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table;

In the syntax:
SELECT is a list of one or more columns
* selects all columns
DISTINCT suppresses duplicates
column|expression selects the named column or the expression
alias gives selected columns different headings
FROM table
Example:
select * from employees

2. Where
- Objectives:
o Restrict the rows that are returned by using the WHERE clause
o The WHERE clause follows the FROM clause
- Syntax:
SELECT *|{[DISTINCT] column|expression [alias],...}
FROM table
[WHERE condition(s)];

In the syntax:
WHERE restricts the query to rows that meet a condition
condition is composed of column names, expressions, constants, and a comparison
operator
Example:
SELECT employee_id, last_name, job_id, department_id
FROM employees
WHERE department_id = 90 ;
SELECT *
FROM employees
WHERE hire_date>='17-jun-1989';

- Arithmetic operators

- Comparison Conditions

- Logical Conditions
- Rules of Precedence

Example:

SELECT last_name, job_id, salary


FROM employees
WHERE job_id = 'SA_REP'
OR job_id = 'AD_PRES'
AND salary > 15000;
 The first condition is that the job ID is AD_PRES and the salary is greater
than $15,000.
 The second condition is that the job ID is SA_REP.

- Sorting
Sort retrieved rows with the ORDER BY clause:
o ASC: ascending order, default
o DESC: descending order
The ORDER BY clause comes last in the SELECT statement
Syntax
SELECT expr
FROM table
[WHERE condition(s)]
[ORDER BY {column, expr, numeric_position} [ASC|DESC]];
In the syntax:
ORDER BY specifies the order in which the retrieved rows are displayed
ASC orders the rows in ascending order (this is the default order)
DESC orders the rows in descending order
The default sort order is ascending:
- Numeric values are displayed with the lowest values first (for example, 1 to 999).
- Date values are displayed with the earliest value first (for example, 01-JAN-92
before
- 01-JAN-95).
- Character values are displayed in alphabetical order (for example, A first and Z
last).
- Null values are displayed last for ascending sequences and first for descending
sequences.
- You can sort by a column that is not in the SELECT list.
3. Insert statement
- Add new rows to a table by using the INSERT statement
- With this syntax, only one row is inserted at a time.
- Syntax:
INSERT INTO table [(column [, column...])]
VALUES (value [, value...]);

In the syntax:
table is the name of the table
column is the name of the column in the table to populate
value is the corresponding value for the column
- Example:

INSERT INTO job_grades VALUES ('A', 1000, 2999);


Insert into job_grades
Select * from job_grades_bk
4. Update statement
- Modify existing rows with the UPDATE statement
- Update more than one row at a time (if required).
- Syntax:
UPDATE table
SET column = value [, column = value, ...]
[WHERE condition];

In the syntax:
table is the name of the table
column is the name of the column in the table to populate
value is the corresponding value or subquery for the column
condition identifies the rows to be updated and is composed of column names,
expressions, constants, subqueries, and comparison operators
- Example:
UPDATE employees
SET department_id = 70
WHERE employee_id = 113;

UPDATE copy_emp
SET department_id = 110;

UPDATE employees –- update with subquery


SET
job_id = (SELECT job_id FROM employees WHERE employee_id = 205),
salary = (SELECT salary FROM employees WHERE employee_id = 205)
WHERE employee_id = 114;
5. Delete statement

You can remove existing rows from a table by using the DELETE statement
- Syntax:
DELETE [FROM] table
[WHERE condition];
In the syntax:
table is the table name
condition identifies the rows to be deleted and is composed of column names,
expressions, constants, subqueries, and comparison operators
- Example:
DELETE FROM departments
WHERE department_name = 'Finance';
DELETE FROM copy_emp;
6. Joining Table
- Equijoins: Equijoins are also called simple joins or inner joins. Equi Join is a
kind of join where condition used is equal(=) sign
Syntax:
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column1 = table2.column2;

In the syntax:
table1.column denotes the table and column from which data is retrieved
table1.column1 = is the condition that joins (or relates) the tables together
table2.column2

- Non-Equijoins
An nonequi join is an inner join statement that uses an unequal operation (i.e: <>,
>, <, !=, BETWEEN, etc.) to match rows from different tables
Example:
SELECT e.last_name, e.salary, j.grade_level
FROM employees e, job_grades j
WHERE e.salary
BETWEEN j.lowest_sal AND j.highest_sal;
- Outer Joins
You use an outer join to see rows that do not meet the join condition.

The outer join operator is the plus sign (+).


Syntax:
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column(+) = table2.column;
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column = table2.column(+);
In the syntax:
table1.column = is the condition that joins (or relates) the tables together
table2.column (+) is the outer join symbol, which can be placed on either side of the
WHERE clause condition, but not on both sides. (Place the outer join symbol
following the name of the column in the table without the matching rows.)
Example:
select e.employee_id, e.last_name, e.department_id, d.department_name
from employees e left outer join departments d
on e.department_id = d.department_id
where last_name = 'Grant';

select e.employee_id, e.last_name, e.department_id, d.department_name


from employees e, departments d
where e.department_id = d.department_id(+) and last_name = 'Grant';

- Self-Joins: Sometimes you need to join a table to itself.


Example:
To find the name of each employee’s manager, you need to join the EMPLOYEES
table to itself; this type of join is called a self-join.

SELECT worker.last_name || ' works for ' || manager.last_name


FROM employees worker, employees manager
WHERE worker.manager_id = manager.employee_id ;
- NATURAL JOIN clause is based on all columns in the two tables that have
the same name
Note: The join can happen on only those columns that have the same names and data
types in both tables. If the columns have the same name but different data types, then
the NATURAL JOIN syntax causes an error.
7. Practices
a. There are four coding errors in the following statement. Can you identify
them?
SELECT employee_id, last_name
sal x 12 ANNUAL SALARY
FROM employees;
b. The following select statement executes successfully: True/False
SELECT last_name, job_id, salary AS Sal
FROM employees;
c. Result of following statement
select null * 2 from dual
select null || 'name' from dual
d. The HR department needs a report of all employees. Write a query to display
the last name, department number, and department name for all employees.
e. The HR department needs a report of all department with the following
format: Department_Name + “, it's assigned Manager Id:” + Manager_ID
Example: “Administration, it's assigned Manager Id: 200”

=> Result:

f. The HR department needs a report on job grades and salaries. To familiarize


yourself with the JOB_GRADES table, first show the structure of the
JOB_GRADES table. Then create a query that displays the name, job,
department name, salary, and grade for all employees
g. The HR department wants to determine the names of all employees who were
hired after Davies. Create a query to display the name and hire date of any
employee hired after employee Davies.

h. The HR department needs to find the names and hire dates for all employees
who were hired before their managers, along with their managers’ names and
hire dates
i. Create a report that displays the employee number, last name, and salary of all
employees who earn more than the average salary. Sort the results in order of
ascending salary.

j. Write a query that displays the employee number and last name of all
employees who work in a department with any employee whose last name
contains a “u”.

k. Create a report for HR that displays the last name and salary of every
employee who reports to King.

l. Create a report for HR that displays the department number, last name, and job
ID for every employee in the Executive department.
m. The HR department needs a report with the following specifications:
o Last name and department ID of all the employees from the EMPLOYEES
table, regardless of whether or not they belong to a department
o Department ID and department name of all the departments from the
DEPARTMENTS table, regardless of whether or not they have employees
working in them
Write a compound query to accomplish this.

n. Create table My_Employee from Employee table.


o Create Insert statement to insert the following row:

o Change the last name of employee 207 to FPTS


o Confirm your changes to the table.
o Delete TEST from My_Employee table
o Commit all pending changes

You might also like