0% found this document useful (0 votes)
60 views20 pages

Chapter 3 - SQL Statement

The document summarizes SQL statements including the SELECT, WHERE, INSERT, UPDATE, DELETE, JOIN, and ORDER BY statements. It provides the syntax and examples of each statement type. It also includes exercises to practice different SQL clauses and functions.

Uploaded by

Abdelaz IDRISSOU
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
60 views20 pages

Chapter 3 - SQL Statement

The document summarizes SQL statements including the SELECT, WHERE, INSERT, UPDATE, DELETE, JOIN, and ORDER BY statements. It provides the syntax and examples of each statement type. It also includes exercises to practice different SQL clauses and functions.

Uploaded by

Abdelaz IDRISSOU
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 20

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 //loại các bản sao chỉ lấy bản ghi
column|expression selects the named column or the expression
alias gives selected columns different headings
FROM table
Example:
select * from employees

By lines I assume you mean rows in the table person. What you're looking for is:
select p.name
from person p
where p.name LIKE '%A%'; --contains the character 'A'
The above is case sensitive. For a case insensitive search, you can do:

select p.name
from person p
where UPPER(p.name) LIKE '%A%'; --contains the character 'A' or 'a'
For the special character, you can do:

select p.name
from person p
where p.name LIKE '%'||chr(8211)||'%'; --contains the character chr(8211)

1.1. Distinct:
- Sau khi lấy dữ liệu ra thì distinct sẽ loại bỏ các bản ghi trùng nhau (toàn bộ cả hàng)

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
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

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.last_name, e.department_id, d.department_name
FROM employees e, departments d
WHERE e.department_id(+) = d.department_id ;
- 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 ;

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.
7.1. Select để hiển thị thà nh 1 câ u
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:
7.2. Câ u Select lồ ng
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

BÀI LÀM:

7.3. Định dạ ng ngà y/thá ng/nă m khi select


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.
BÀI LÀM:

'DD - Mon - YYYY': 10 – Jan – 1997


'DD - Month - YYYY': 10 - January – 1997

==========================================================
======================================================
7.4. So sá nh ngà y trong select
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

BÀI LÀM:

7.5. Câ u lệnh sort (asc, desc) – order by


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.
BÀI LÀM:

Viết gọn lại thành:


7.6. Format string: contain
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”.

BÀI LÀM:
7.7. Like as
e. Create a report for HR that displays the last name and salary of every
employee who reports to King.

f. Create a report for HR that displays the department number, last name, and job
ID for every employee in the Executive department.

BÀI LÀM:
7.8. Full join
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.
BÀI LÀM:
g. 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