SQL - P3
SQL - P3
SQL> SELECT *
FROM EMP;
1
EMP Table
3
Selecting Multiple Columns
SQL> SELECT EMPNO, ENAME, JOB
FROM EMP;
4
Reordering Columns During
Selection
SQL> SELECT EMPNO, ENAME, JOB
FROM EMP;
5
Selecting Rows
If you don’t specify a WHERE clause,
all rows will be selected
By specifying a WHERE clause, you
can choose specific rows
6
Ordering Rows in a Sequence
In the relational model, rows have no
particular order
The ORDER BY command is the only way you
can ensure rows will be displayed according
to specific criteria
SQL> SELECT *
FROM DEPT
ORDER BY ENAME, DEPTNO DESC;
8
Selecting Specific Rows:
A Single Condition
In the WHERE clause you can compare a column value
to:
a CHARACTER constant by using single
quotes
where ename = ‘SMITH’
Equal to =
Not equal to != or <>
Greater than >
Greater than or equal >=
Less than <
Less than or equal <=
10
Other Operators
• Equal to any member of the following list IN (list)
Conversely, NOT IN lets you select rows that do not fall in the list
List all employees whose job titles do not begin with the string
SALES.
17
But when;
WHERE DEPT = 30
AND (JOB=‘SALESMAN’ OR SAL>20,000);
18
Introduction to Expressions
You can use SQL like a calculator to
express values with arithmetic operators.
These include:
+ ADD
- SUBTRACT
* MULTIPLY
/ DIVIDE
These operators can be used in just about
any kind of clause, including
SELECT
WHERE
ORDER BY 19
Numeric Expressions
You can use more than one expression in the
same query:
20
Numeric expressions with Multiple Operators
You may need multiple expressions together
Here’s the order of evaluation:
1. Multiplication *
2. Division /
3. Addition +
4. Subtraction -
24
Multiple Criteria for Grouping Rows
How many employee are there in each
department?
SQL> SELECT DEPTNO, COUNT(*)
FROM EMP
GROUP BY DEPTNO;