Lab 1 - DMS
Lab 1 - DMS
2
Structured Query Language
(SQL)
SQL, PL/SQL, and SQL*PLUS
4
Data retrieval command (DRC)
Basic SELECT Statement
SELECT
SELECT *|{[DISTINCT]
*|{[DISTINCT] column|expression
column|expression [alias],...}
[alias],...}
FROM
FROM table;
table;
6
Selecting All Columns
SELECT *
FROM departments;
7
Selecting Specific Columns
* / +
-
• Multiplication and division take priority over addition
and subtraction.
• Operators from the same priority are evaluated from
left to right.
• Parentheses are used to enforce prioritized
evaluation and to clarify statements.
10
Operator Precedence
11
Using Parentheses
13
Null Values
in Arithmetic Expressions
14
Defining a Column Alias
A column alias:
• Renames a column heading
• Is useful with calculations
• Immediately follows the column name
• The optional AS keyword may be used between the
column name and alias
• Requires double quotation marks if it contains spaces
or special characters (such as # or $), or is case
sensitive
15
Using Column Aliases
SELECT last_name AS name, commission_pct comm
FROM employees;
16
Concatenation Operator
A concatenation operator:
• Concatenates columns or character strings to
other columns
• Is represented by two vertical bars (||)
• Creates a resultant column that is a character
expression
17
Using the Concatenation Operator
18
Literal Character Strings
19
Using Literal Character Strings
20
Duplicate Rows
SELECT
SELECT department_id
department_id
FROM
FROM employees;
employees;
21
Eliminating Duplicate Rows
Eliminate duplicate rows by using the DISTINCT keyword
in the SELECT clause.
SELECT DISTINCT department_id
FROM employees;
22
Eliminating Duplicate Rows
23
Restricting Data
Limiting the Rows Selected
25
Using the WHERE Clause
= Equal , < Less than , > Greater than , <> Not equal
26
Character Strings and Dates
27
Using Comparison Conditions
28
Other Comparison Conditions
Operator Meaning
29
The BETWEEN Condition
31
The LIKE Condition
SELECT first_name
FROM employees
WHERE first_name LIKE 'S%';
32
The LIKE Condition
33
Using the LIKE Condition
Escape
SELECT employee_id,
last_name, job_id
FROM employees
WHERE job_id LIKE
'%SA\_%' ESCAPE '\';
34
The NULL Conditions
35
Logical Conditions
Operator Meaning
36
Using the AND Operator
37
Using the OR Operator
38
Using the NOT Operator
39
Sorting Data
The ORDER BY Clause
41
Sorting in Descending Order
42
Sorting by Multiple Columns
• YouSELECT
can order by position, e.g. 2nd column in select clause.
last_name, job_id, department_id, hire_date
FROM employees
ORDER BY 2;
44
Thank You