Chapter 3
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:
- 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:
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;
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.
=> Result:
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.