Beruflich Dokumente
Kultur Dokumente
Basic SQL
SELECT [DISTINCT] * {column | expression [alias], …}
FROM table;
Selecting All Column Rows
SELECT *
FROM departments;
Selecting Specific Columns
SELECT first_name
FROM employees
WHERE first_name LIKE „S%‟;
SELECT last_name
FROM employees
WHERE last_name LIKE „ o%‟;
Rules of Precedence
ORDER BY Clause
II. FUNCTIONS
Character Manipulation Functions
Number Functions
CONCAT – joins values together (you are limited to using two parameters with CONCAT).
SUBSTR – extracts a string of determined length.
LENGTH – shows the length of a string as a numeric value.
INSTR – finds numeric position of a named character.
LPAD – pads the character value right-justified.
RPAD – pads the character value left justified.
TRIM – trims heading or trailing characters.
Date Functions
SELECT last_name,
TO_CHAR(hire_date, „fmDD Month YYYY‟) AS HIREDATE
FROM employees;
SELECT last_name,
TO_CHAR(hire_date, „fmDdspth “of” Month YYYY fmHH:MI:SS AM‟) HIREDATE
FROM employees;
9 – represents a number
0 – forces a zero to be displayed
$ – places a floating dollar sign
L – uses the floating local currency symbol
. – prints a decimal point
, – prints a thousand indicator
GENERAL FUNCTIONS
These functions work with any data type and pertain to using nulls
NVL (expr1, expr2) – converts a null value to an actual value.
NVL2(expr1, expr2, expr3) – if expr1 is not null, nvl2 returns expr2. if expr1 is null, nvl2 returns
expr3.
NULLIF(expr1, expr2) – compares two expressions and returns null if they are equal, or the first
expression if they are not equal.
COALESCE(expr1, expr2, …, exprn) – returns the first non-null expression in the expression list.
Using the NVL Function
SELECT last_name,
NVL(TO_CHAR(manager_id), „No Manager‟)
FROM employees
WHERE manager_id IS NULL;
SELECT last_name,
COALESCE(commission_pct, salary, 10) comm.
FROM employees
ORDER BY commission_pct;
CONDITIONAL EXPRESSIONS
Facilitates the conditional inquiries by doing the work of an IF-THEN-ELSE statement.
Non-Equijoins
Outer Joins
Natural Joins
It selects rows from tables that have equal values in all matched columns. It can also be written
using an equijoin.
SELECT department_id, department_name, location_id, city
FROM departments
NATURAL JOIN locations;
The ON Clause
The join condition for the natural join is basically an equijoin of all columns with the same name.
To specify arbitrary conditions or specify columns to join, the ON clause is used.
This query retrieves all rows in the EMPLOYEES table, which is the left table even if there is no
match in the departments table.
This query retrieves all rows in the DEPARTMENTS table, which is the right table even if there is
no match in the EMPLOYEES table.
This query retrieves all rows in the EMPLOYEES table, even if there is no match in the
DEPARTMENTS table. It also retrieves all rows in the DEPARTMENTS table, even if there is no
match in the EMPLOYEES table.
IV. GROUP FUNCTIONS
GROUP FUNCTIONS
SELECT COUNT(*)
FROM employees
WHERE department_id = 50;
SELECT COUNT(commission_pct)
FROM employees
WHERE department_id = 80;
SELECT COUNT(department_id)
FROM employees;
All group functions ignore null values in the column. In the example, the average is calculated
based only on the rows in the table where a valid value is stored in the COMMISSION_PCT
column.
SELECT AVG(commission_pct)
FROM employees;
The NVL function forces group functions to include null values. In the example, the average is
calculated based on all rows in the table, regardless of whether null values are stored in the
COMMISSION_PCT column.
SELECT MAX(AVG(salary))
FROM employees
GROUP BY department_id;