Select FROM Dept (Dept Este Tabelul) : Selecting All Columns
Select FROM Dept (Dept Este Tabelul) : Selecting All Columns
SELECT *
FROM dept; ( dept este tabelul )
=>
DEPTNO
--------10
20
30
40
DNAME
-------------ACCOUNTING
RESEARCH
SALES
OPERATIONS
LOC
------------NEW YORK
DALLAS
CHICAGO
BOSTON
DEPTNO
--------10
20
30
40
LOC
------------NEW YORK
DALLAS
CHICAGO
BOSTON
ENAME
SAL
---------- --------KING
5000
BLAKE
2850
CLARK
2450
JONES
2975
MARTIN
1250
ALLEN
1600
...
14 rows selected.
SAL+300
--------5300
3150
2750
3275
1550
1900
Operator Precedence * / +
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.
SELECT ename, sal, 12*sal+100
FROM emp;
=>
ENAME
SAL
---------- --------KING
5000
BLAKE
2850
CLARK
2450
JONES
2975
MARTIN
1250
ALLEN
1600
...
14 rows selected.
12*SAL+100
---------60100
34300
29500
35800
15100
19300
NAME
SALARY
------------- --------...
SELECT ename "Name",
sal*12 "Annual Salary"
FROM emp;
=>
Name
Annual Salary
------------- ------------...
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
SELECT ename||job AS "Employees"
FROM emp;
=>
Employees
------------------KINGPRESIDENT
BLAKEMANAGER
CLARKMANAGER
JONESMANAGER
MARTINSALESMAN
ALLENSALESMAN
...
14 rows selected.
Employee Details
------------------------KING is a PRESIDENT
BLAKE is a MANAGER
CLARK is a MANAGER
JONES is a MANAGER
MARTIN is a SALESMAN
...
14 rows selected.
Eliminating Duplicate Rows
SELECT DISTINCT deptno
FROM emp;
=>
DEPTNO
--------10
20
30
WHERE Clause
The WHERE clause follows the FROM clause.
SELECT ename, job, deptno
FROM emp
WHERE job = 'CLERK';
=>
ENAME
---------JAMES
SMITH
ADAMS
MILLER
JOB
--------CLERK
CLERK
CLERK
CLERK
DEPTNO
--------30
20
20
10
Comparison Operators
Operator
Meaning
=
Equal to
>
Greater than
>=
Greater than or equal to
<
Less than
<=
Less than or equal to
<>
Not equal to
ENAME
SAL
COMM
---------- --------- --------MARTIN
1250
1400
Other Comparison Operators
Operator
Meaning
BETWEEN
Between two values (inclusive)
...AND...
IN (list)
Match any of a list of values
LIKE
Match a character pattern
IS NULL
Is a null value
BETWEEN Operator
SELECT ename, sal
FROM emp
WHERE sal BETWEEN 1000 AND 1500;
=>
ENAME
---------MARTIN
TURNER
WARD
ADAMS
MILLER
SAL
--------1250
1500
1250
1100
1300
Lower Higher
limit limit
IN Operator
SELECT empno, ename, sal, mgr
FROM emp
WHERE mgr IN (7902, 7566, 7788);
=>
EMPNO
--------7902
7369
7788
7876
ENAME
---------FORD
SMITH
SCOTT
ADAMS
SAL
--------3000
800
3000
1100
MGR
--------7566
7902
7566
7788
LIKE Operator
SELECT ename
FROM emp
WHERE ename LIKE 'S%';
The SELECT statement above returns the employee name from the EMP table for any
employee whose name begins with an S. Note the uppercase S. Names beginning with an
s will not be returned.
The following example displays names and hiredates of all employees who joined in 81.
SELECT ename, hiredate
FROM emp
WHERE hiredate LIKE %81;
The % and _ symbols can be used in any combination with literal characters. The example on
the slide displays the names of all employees whose name has an A as the second
character.
SELECT ename
FROM emp
WHERE ename LIKE '_A%';
=>
ENAME
---------JAMES
WARD
The ESCAPE option
When you need to have an exact match for the actual % and _ characters, use the
ESCAPE option. This option specifies what the ESCAPE character is. To display the names
of employees whose name contains A_B, use the following SQL statement:
SELECT name
FROM emp
WHERE name LIKE %A\_%B ESCAPE \;
The ESCAPE option identifies the backlash (\) as the escape character. In the pattern, the
escape character precedes the underscore (_). This causes the Oracle8 Server to interpret the
underscore literally.
IS NULL Operator
SELECT ename, job, comm
FROM emp
WHERE comm IS NULL;
=>
ENAME
JOB
COMM
-------- ----------- -----KING
PRESIDENT
BLAKE
MANAGER
CLARK
MANAGER
...
Logical Operators
AND Operator AND requires both conditions to be TRUE.
SELECT empno, ename, job, sal
FROM emp
WHERE sal>=1100
AND
job='CLERK';
=>
EMPNO
--------7876
7934
ENAME
---------ADAMS
MILLER
JOB
--------CLERK
CLERK
SAL
--------1100
1300
ENAME
---------KING
MARTIN
ALLEN
TURNER
WARD
JOB
--------PRESIDENT
SALESMAN
SALESMAN
SALESMAN
SALESMAN
Rules of Precedence
Example of Precedence of AND Operator
In the example, there are two conditions:
The first condition is that job is PRESIDENT and salary is greater than 1500.
The second condition is that job is SALESMAN.
Therefore, the SELECT statement reads as follows: Select the row if an employee is
a PRESIDENT and earns more than $1500 or if the employee is a SALESMAN.
SELECT ename, job, sal
FROM emp
WHERE job='SALESMAN'
OR 2 job='PRESIDENT'
AND 1 sal>1500;
=>
ENAME
---------KING
MARTIN
ALLEN
TURNER
WARD
JOB
--------PRESIDENT
SALESMAN
SALESMAN
SALESMAN
SALESMAN
SAL
--------5000
1250
1600
1500
1250
Using Parentheses
In the example, there are two conditions:
The first condition is that job is PRESIDENT or SALESMAN.
The second condition is that salary is greater than 1500.
Therefore, the SELECT statement reads as follows: Select the row if an employee is
a PRESIDENT or a SALESMAN and if the employee earns more than $1500.
SELECT ename, job, sal
FROM emp
WHERE (job='SALESMAN'
OR
job='PRESIDENT')
AND
sal>1500;
=>
ENAME
JOB
---------- --------SMITH
CLERK
ALLEN
SALESMAN
...
14 rows selected.
DEPTNO
--------20
30
HIREDATE
--------17-DEC-80
20-FEB-81
Sorting by Column Alias You can use a column alias in the ORDER BY clause.
The example sorts the data by annual salary.
SELECT
empno, ename, sal*12 annsal
FROM
emp
ORDER BY annsal;
SQL Functions
Single-Row Functions
Manipulate data items
Accept arguments and return one value
Act on each row returned
Return one result per row
May modify the data type
Can be nested
Character Functions
Function Purpose
LOWER (column|expression) - Converts alpha character values to lowercase
LOWER (BLAKE) -> blake
UPPER (column|expression) - Converts alpha character values to uppercase
INITCAP (column|expression) - Converts alpha character values to uppercase for the first
letter of each word, all other letters in lowercase
INITCAP (BLAKE) -> Blake
CONCAT (column1|expression1, column2|expression2) - Concatenates the first character
value to the second character value. Equivalent to concatenation operator (||)
CONCAT ('Good', 'String') -> GoodString
SUBSTR (column|expression, m, [n]) - Returns specified characters from character value
starting at character position m, n characters long. If m is negative, the count starts from the
end of the character value. If n is omitted, all characters to the end of the string are returned
SUBSTR ('String', 1, 3) -> Str
LENGTH (column|expression) - Returns the number of characters in value
LENGTH ('String') -> 6
INSTR (column|expression, m) - Returns the numeric position of a named character
INSTR ('String', 'r') -> 3
LPAD (column|expression, n, string) - Pads the character value right-justified to a total
width of n character positions
LPAD (sal, 10,'*') -> ******5000
Number Functions
ROUND: Rounds value to specified decimal
ROUND (45.926, 2) -> 45.93
ROUND (45.923, 0) -> 46
ROUND (45.923, -1) -> 50
TRUNC: Truncates value to specified decimal
TRUNC (45.926, 2) -> 45.92
TRUNC (45.923) -> 45
TRUNC (45.923,-1) -> 40
MOD: Returns remainder of division
MOD (1600, 300) -> 100
!!! SYSDATE is a function returning date and time.
Display the current date using the DUAL table.
JOINS
Use a join to query data from more than one table.
SELECT table.column, table.column
FROM table1, table2
WHERE table1.column1 = table2.column2;
Equijoin Frequently, this type of joins involve primary and foreign key
complements.
SELECT emp.empno, emp.ename, emp.deptno,
dept.deptno, dept.loc
FROM emp, dept
WHERE emp.deptno=dept.deptno;
=>
DEPTNO
-----10
30
10
20
LOC
--------NEW YORK
CHICAGO
NEW YORK
DALLAS
WORKER.ENAME||'WORKSFOR'||MANAG
------------------------------BLAKE works for KING
CLARK works for KING
JONES works for KING
MARTIN works for BLAKE
...
13 rows selected.