SQL-1 Introduction
SQL-1 Introduction
SQL-1 Introduction
(SQL)
Note:
The slides are adapted from “Oracle Corporation”
Introduction
SQL statement
is entered Statement is sent to
SQL> SELECT loc database
2 FROM dept;
Database
Data is displayed
LOC
-------------
NEW YORK
DALLAS
CHICAGO
BOSTON
SQL Statements
Tables Used in the Course
EMP
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
--------- ---------- --------- --------- --------- --------- --------- ---------
7839 KING PRESIDENT 17-NOV-81 5000 10
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
DEPT 7900 JAMES CLERK 7698 03-DEC-81 950 30
DEPTNO 7521
DNAMEWARD SALESMAN
LOC 7698 22-FEB-81 1250 500 30
--------- 7902 FORD
-------------- ANALYST
---------- 7566 03-DEC-81 SALGRADE
3000 20
10 7369 SMITH
ACCOUNTING CLERK
NEW YORK 7902 17-DEC-80 800
GRADE LOSAL 20
HISAL
20 7788 SCOTT
RESEARCH ANALYST
DALLAS 7566 09-DEC-82 3000 --------- ---------
--------- 20
30 7876
SALESADAMS CLERK
CHICAGO 7788 12-JAN-83 1100 1 700 20
1200
40 7934 MILLER
OPERATIONS CLERK
BOSTON 7782 23-JAN-82 1300 2 1201 10
1400
3 1401 2000
4 2001 3000
5 3001 9999
Querying the Tables
• The Select statement is the building block for querying the
tables:
• Syntax:
SELECT [DISTINCT] {* | column [alias],...}
FROM table (s)
[WHERE clause]
[ORDER BY clause]
[GROUP BY clause]
[HAVING clause]
• SELECT identifies what columns
• FROM identifies which table
Capabilities of SQL SELECT Statements
Selection Projection
Table 1 Table 1
Join
Table 1 Table 2
Writing SQL Statements
SQL> SELECT *
2 FROM dept;
DEPTNO LOC
--------- -------------
10 NEW YORK
20 DALLAS
30 CHICAGO
40 BOSTON
Arithmetic Expressions
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
Using Arithmetic Operators
_
* / +
• Multiplication and division take priority over
addition and subtraction.
• Operators of the same priority are evaluated from
left to right.
• Parentheses are used to force prioritized evaluation
and to clarify statements.
Defining a Column Alias
NAME SALARY
------------- ---------
...
DEPTNO
---------
10
30
10
20
...
14 rows selected.
Eliminating Duplicate Rows
DEPTNO
---------
10
20
30