ΠΠ΄ΡΠ°Π²ΡΡΠ²ΡΠΉ, Π₯Π°Π±Ρ!
ΠΠΎΡ ΡΠΆΠ΅ Π±ΠΎΠ»Π΅Π΅ 3-Ρ Π»Π΅Ρ Ρ ΠΏΡΠ΅ΠΏΠΎΠ΄Π°Ρ SQL Π² ΡΠ°Π·Π½ΡΡ ΡΡΠ΅Π½ΠΈΠ½Π³ ΡΠ΅Π½ΡΡΠ°Ρ , ΠΈ ΠΎΠ΄Π½ΠΈΠΌ ΠΈΠ· ΠΌΠΎΠΈΡ Π½Π°Π±Π»ΡΠ΄Π΅Π½ΠΈΠΉ ΡΠ²Π»ΡΠ΅ΡΡΡ ΡΠΎ, ΡΡΠΎ ΡΡΡΠ΄Π΅Π½ΡΡ ΠΎΡΠ²Π°ΠΈΠ²Π°ΡΡ ΠΈ ΠΏΠΎΠ½ΠΈΠΌΠ°ΡΡ SQL Π»ΡΡΡΠ΅, Π΅ΡΠ»ΠΈ ΡΡΠ°Π²ΠΈΡΡ ΠΏΠ΅ΡΠ΅Π΄ Π½ΠΈΠΌΠΈ Π·Π°Π΄Π°ΡΡ, Π° Π½Π΅ ΠΏΡΠΎΡΡΠΎ ΡΠ°ΡΡΠΊΠ°Π·ΡΠ²Π°ΡΡ ΠΎ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΡΡΡΡ ΠΈ ΡΠ΅ΠΎΡΠ΅ΡΠΈΡΠ΅ΡΠΊΠΈΡ ΠΎΡΠ½ΠΎΠ²Π°Ρ .
Π ΡΡΠΎΠΉ ΡΡΠ°ΡΡΠ΅ Ρ ΠΏΠΎΠ΄Π΅Π»ΡΡΡ Ρ Π²Π°ΠΌΠΈ ΡΠ²ΠΎΠΈΠΌ ΡΠΏΠΈΡΠΊΠΎΠΌ Π·Π°Π΄Π°Ρ, ΠΊΠΎΡΠΎΡΡΠ΅ Ρ Π΄Π°Ρ ΡΡΡΠ΄Π΅Π½ΡΠ°ΠΌ Π² ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ Π΄ΠΎΠΌΠ°ΡΠ½Π΅Π³ΠΎ Π·Π°Π΄Π°Π½ΠΈΡ ΠΈ Π½Π°Π΄ ΠΊΠΎΡΠΎΡΡΠΌΠΈ ΠΌΡ ΠΏΡΠΎΠ²ΠΎΠ΄ΠΈΠΌ ΡΠ°Π·Π½ΠΎΠ³ΠΎ ΡΠΎΠ΄Π° Π±ΡΠ΅ΠΉΠ½ΡΡΠΎΡΠΌΡ, ΡΡΠΎ ΠΏΡΠΈΠ²ΠΎΠ΄ΠΈΡ ΠΊ Π³Π»ΡΠ±ΠΎΠΊΠΎΠΌΡ ΠΈ ΡΠ΅ΡΠΊΠΎΠΌΡ ΠΏΠΎΠ½ΠΈΠΌΠ°Π½ΠΈΡ SQL.
SQL (ΛΙsΛkjuΛΙl; Π°Π½Π³Π». structured query language β Β«ΡΠ·ΡΠΊ ΡΡΡΡΠΊΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΡ
Π·Π°ΠΏΡΠΎΡΠΎΠ²Β») β Π΄Π΅ΠΊΠ»Π°ΡΠ°ΡΠΈΠ²Π½ΡΠΉ ΡΠ·ΡΠΊ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠΈΡΠΎΠ²Π°Π½ΠΈΡ, ΠΏΡΠΈΠΌΠ΅Π½ΡΠ΅ΠΌΡΠΉ Π΄Π»Ρ ΡΠΎΠ·Π΄Π°Π½ΠΈΡ, ΠΌΠΎΠ΄ΠΈΡΠΈΠΊΠ°ΡΠΈΠΈ ΠΈ ΡΠΏΡΠ°Π²Π»Π΅Π½ΠΈΡ Π΄Π°Π½Π½ΡΠΌΠΈ Π² ΡΠ΅Π»ΡΡΠΈΠΎΠ½Π½ΠΎΠΉ Π±Π°Π·Π΅ Π΄Π°Π½Π½ΡΡ
, ΡΠΏΡΠ°Π²Π»ΡΠ΅ΠΌΠΎΠΉ ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡΠ΅ΠΉ ΡΠΈΡΡΠ΅ΠΌΠΎΠΉ ΡΠΏΡΠ°Π²Π»Π΅Π½ΠΈΡ Π±Π°Π·Π°ΠΌΠΈ Π΄Π°Π½Π½ΡΡ
.
ΠΠΎΡΠΈΡΠ°ΡΡ ΠΎΠ± SQL ΠΌΠΎΠΆΠ½ΠΎ ΠΈΠ· ΡΠ°Π·Π½ΡΡ
ΠΠ°Π½Π½Π°Ρ ΡΡΠ°ΡΡΡ Π½Π΅ ΠΏΡΠ΅ΡΠ»Π΅Π΄ΡΠ΅Ρ ΡΠ΅Π»ΠΈ ΠΎΠ±ΡΡΠΈΡΡ Π²Π°Ρ SQL Ρ Π½ΡΠ»Ρ.
ΠΡΠ°ΠΊ, ΠΏΠΎΠ΅Ρ
Π°Π»ΠΈ.
ΠΡΠ΄Π΅ΠΌ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²ΡΡ Π²ΡΠ΅ΠΌ ΠΈΠ·Π²Π΅ΡΡΠ½ΡΡ
ΠΡΠΌΠ΅ΡΡ ΡΡΠΎ ΠΌΡ Π±ΡΠ΄Π΅ΠΌ ΡΠ°ΡΡΠΌΠ°ΡΡΠΈΠ²Π°ΡΡ ΡΠΎΠ»ΡΠΊΠΎ Π·Π°Π΄Π°ΡΠΈ Π½Π° SELECT. Π’ΡΡ Π½Π΅Ρ Π·Π°Π΄Π°Ρ Π½Π° DML ΠΈ DDL.
ΠΠ°Π΄Π°ΡΠΈ
Restricting and Sorting Data
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Ρ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΠ΅ΠΉ ΠΎΠ±ΠΎ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠ°Ρ
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT * FROM employees
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Ρ ΠΈΠΌΠ΅Π½Π΅ΠΌ ‘David’
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE first_name = 'David';
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Ρ job_id ΡΠ°Π²Π½ΡΠΌ ‘IT_PROG’
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE job_id = 'IT_PROG'
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΈΠ· 50Π³ΠΎ ΠΎΡΠ΄Π΅Π»Π° (department_id) Ρ Π·Π°ΡΠΏΠ»Π°ΡΠΎΠΉ(salary), Π±ΠΎΠ»ΡΡΠ΅ΠΉ 4000
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE department_id = 50 AND salary > 4000;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΈΠ· 20Π³ΠΎ ΠΈ ΠΈΠ· 30Π³ΠΎ ΠΎΡΠ΄Π΅Π»Π° (department_id)
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE department_id = 20 OR department_id = 30;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Ρ ΠΊΠΎΡΠΎΡΡΡ
ΠΏΠΎΡΠ»Π΅Π΄Π½ΡΡ Π±ΡΠΊΠ²Π° Π² ΠΈΠΌΠ΅Π½ΠΈ ΡΠ°Π²Π½Π° ‘a’
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE first_name LIKE '%a';
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΈΠ· 50Π³ΠΎ ΠΈ ΠΈΠ· 80Π³ΠΎ ΠΎΡΠ΄Π΅Π»Π° (department_id) Ρ ΠΊΠΎΡΠΎΡΡΡ
Π΅ΡΡΡ Π±ΠΎΠ½ΡΡ (Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ Π² ΠΊΠΎΠ»ΠΎΠ½ΠΊΠ΅ commission_pct Π½Π΅ ΠΏΡΡΡΠΎΠ΅)
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE (department_id = 50 OR department_id = 80)
AND commission_pct IS NOT NULL;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Ρ ΠΊΠΎΡΠΎΡΡΡ
Π² ΠΈΠΌΠ΅Π½ΠΈ ΡΠΎΠ΄Π΅ΡΠΆΠ°ΡΡΡ ΠΌΠΈΠ½ΠΈΠΌΡΠΌ 2 Π±ΡΠΊΠ²Ρ ‘n’
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE first_name LIKE '%n%n%';
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Ρ ΠΊΠΎΡΠΎΡΡΡ
Π΄Π»ΠΈΠ½Π° ΠΈΠΌΠ΅Π½ΠΈ Π±ΠΎΠ»ΡΡΠ΅ 4 Π±ΡΠΊΠ²
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE first_name LIKE '%_____%';
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Ρ ΠΊΠΎΡΠΎΡΡΡ
Π·Π°ΡΠΏΠ»Π°ΡΠ° Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ Π² ΠΏΡΠΎΠΌΠ΅ΠΆΡΡΠΊΠ΅ ΠΎΡ 8000 Π΄ΠΎ 9000 (Π²ΠΊΠ»ΡΡΠΈΡΠ΅Π»ΡΠ½ΠΎ)
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE salary BETWEEN 8000 AND 9000;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Ρ ΠΊΠΎΡΠΎΡΡΡ
Π² ΠΈΠΌΠ΅Π½ΠΈ ΡΠΎΠ΄Π΅ΡΠΆΠΈΡΡΡ ΡΠΈΠΌΠ²ΠΎΠ» ‘%’
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE first_name LIKE '%%%' ESCAPE '';
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ID ΠΌΠ΅Π½Π΅Π΄ΠΆΠ΅ΡΠΎΠ²
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT DISTINCT manager_id
FROM employees
WHERE manager_id IS NOT NULL;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ ΡΠ°Π±ΠΎΡΠ½ΠΈΠΊΠΎΠ² Ρ ΠΈΡ
ΠΏΠΎΠ·ΠΈΡΠΈΡΠΌΠΈ Π² ΡΠΎΡΠΌΠ°ΡΠ΅: Donald(sh_clerk)
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT first_name || '(' || LOWER (job_id) || ')' employee FROM employees;
Using Single-Row Functions to Customize Output
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Ρ ΠΊΠΎΡΠΎΡΡΡ
Π΄Π»ΠΈΠ½Π° ΠΈΠΌΠ΅Π½ΠΈ Π±ΠΎΠ»ΡΡΠ΅ 10 Π±ΡΠΊΠ²
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE LENGTH (first_name) > 10;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Ρ ΠΊΠΎΡΠΎΡΡΡ
Π² ΠΈΠΌΠ΅Π½ΠΈ Π΅ΡΡΡ Π±ΡΠΊΠ²Π° ‘b’ (Π±Π΅Π· ΡΡΠ΅ΡΠ° ΡΠ΅Π³ΠΈΡΡΡΠ°)
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE INSTR (LOWER (first_name), 'b') > 0;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Ρ ΠΊΠΎΡΠΎΡΡΡ
Π² ΠΈΠΌΠ΅Π½ΠΈ ΡΠΎΠ΄Π΅ΡΠΆΠ°ΡΡΡ ΠΌΠΈΠ½ΠΈΠΌΡΠΌ 2 Π±ΡΠΊΠ²Ρ ‘a’
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE INSTR (LOWER (first_name),'a',1,2) > 0;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Π·Π°ΡΠΏΠ»Π°ΡΠ° ΠΊΠΎΡΠΎΡΡΡ
ΠΊΡΠ°ΡΠ½Π° 1000
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE MOD (salary, 1000) = 0;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΠΏΠ΅ΡΠ²ΠΎΠ΅ 3Ρ
Π·Π½Π°ΡΠ½ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ ΡΠ΅Π»Π΅ΡΠΎΠ½Π½ΠΎΠ³ΠΎ Π½ΠΎΠΌΠ΅ΡΠ° ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠ° Π΅ΡΠ»ΠΈ Π΅Π³ΠΎ Π½ΠΎΠΌΠ΅Ρ Π² ΡΠΎΡΠΌΠ°ΡΠ΅ Π₯Π₯Π₯.Π₯Π₯Π₯.Π₯Π₯Π₯Π₯
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT phone_number, SUBSTR (phone_number, 1, 3) new_phone_number
FROM employees
WHERE phone_number LIKE '___.___.____';
Π’Π°Π±Π»ΠΈΡΠ° Departments. ΠΠΎΠ»ΡΡΠΈΡΡ ΠΏΠ΅ΡΠ²ΠΎΠ΅ ΡΠ»ΠΎΠ²ΠΎ ΠΈΠ· ΠΈΠΌΠ΅Π½ΠΈ Π΄Π΅ΠΏΠ°ΡΡΠ°ΠΌΠ΅Π½ΡΠ° Π΄Π»Ρ ΡΠ΅Ρ
Ρ ΠΊΠΎΠ³ΠΎ Π² Π½Π°Π·Π²Π°Π½ΠΈΠΈ Π±ΠΎΠ»ΡΡΠ΅ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΡΠ»ΠΎΠ²Π°
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT department_name,
SUBSTR (department_name, 1, INSTR (department_name, ' ')-1)
first_word
FROM departments
WHERE INSTR (department_name, ' ') > 0;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΠΈΠΌΠ΅Π½Π° ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Π±Π΅Π· ΠΏΠ΅ΡΠ²ΠΎΠΉ ΠΈ ΠΏΠΎΡΠ»Π΅Π΄Π½Π΅ΠΉ Π±ΡΠΊΠ²Ρ Π² ΠΈΠΌΠ΅Π½ΠΈ
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT first_name, SUBSTR (first_name, 2, LENGTH (first_name) - 2) new_name
FROM employees;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Ρ ΠΊΠΎΡΠΎΡΡΡ
ΠΏΠΎΡΠ»Π΅Π΄Π½ΡΡ Π±ΡΠΊΠ²Π° Π² ΠΈΠΌΠ΅Π½ΠΈ ΡΠ°Π²Π½Π° ‘m’ ΠΈ Π΄Π»ΠΈΠ½Π½ΠΎΠΉ ΠΈΠΌΠ΅Π½ΠΈ Π±ΠΎΠ»ΡΡΠ΅ΠΉ 5ΡΠΈ
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE SUBSTR (first_name, -1) = 'm' AND LENGTH(first_name)>5;
Π’Π°Π±Π»ΠΈΡΠ° Dual. ΠΠΎΠ»ΡΡΠΈΡΡ Π΄Π°ΡΡ ΡΠ»Π΅Π΄ΡΡΡΠ΅ΠΉ ΠΏΡΡΠ½ΠΈΡΡ
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT NEXT_DAY (SYSDATE, 'FRIDAY') next_friday FROM DUAL;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠ°Π±ΠΎΡΠ°ΡΡ Π² ΠΊΠΎΠΌΠΏΠ°Π½ΠΈΠΈ Π±ΠΎΠ»ΡΡΠ΅ 17 Π»Π΅Ρ
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE MONTHS_BETWEEN (SYSDATE, hire_date) / 12 > 17;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Ρ ΠΊΠΎΡΠΎΡΡΡ
ΠΏΠΎΡΠ»Π΅Π΄Π½Ρ ΡΠΈΡΡΠ° ΡΠ΅Π»Π΅ΡΠΎΠ½Π½ΠΎΠ³ΠΎ Π½ΠΎΠΌΠ΅ΡΠ° Π½Π΅ΡΠ΅ΡΠ½Π°Ρ ΠΈ ΡΠΎΡΡΠΎΠΈΡ ΠΈΠ· 3Π΅Ρ
ΡΠΈΡΠ΅Π» ΡΠ°Π·Π΄Π΅Π»Π΅Π½Π½ΡΡ
ΡΠΎΡΠΊΠΎΠΉ
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE MOD (SUBSTR (phone_number, -1), 2) != 0
AND INSTR (phone_number,'.',1,3) = 0;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Ρ ΠΊΠΎΡΠΎΡΡΡ
Π² Π·Π½Π°ΡΠ΅Π½ΠΈΠΈ job_id ΠΏΠΎΡΠ»Π΅ Π·Π½Π°ΠΊΠ° ‘_’ ΠΊΠ°ΠΊ ΠΌΠΈΠ½ΠΈΠΌΡΠΌ 3 ΡΠΈΠΌΠ²ΠΎΠ»Π° Π½ΠΎ ΠΏΡΠΈ ΡΡΠΎΠΌ ΡΡΠΎ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΠΏΠΎΡΠ»Π΅ ‘_’ Π½Π΅ ΡΠ°Π²Π½ΠΎ ‘CLERK’
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE LENGTH (SUBSTR (job_id, INSTR (job_id, '_') + 1)) > 3
AND SUBSTR (job_id, INSTR (job_id, '_') + 1) != 'CLERK';
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Π·Π°ΠΌΠ΅Π½ΠΈΠ² Π² Π·Π½Π°ΡΠ΅Π½ΠΈΠΈ PHONE_NUMBER Π²ΡΠ΅ ‘.’ Π½Π° ‘-‘
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT phone_number, REPLACE (phone_number, '.', '-') new_phone_number
FROM employees;
Using Conversion Functions and Conditional Expressions
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΊΠΎΡΠΎΡΡΠ΅ ΠΏΡΠΈΡΠ»ΠΈ Π½Π° ΡΠ°Π±ΠΎΡΡ Π² ΠΏΠ΅ΡΠ²ΡΠΉ Π΄Π΅Π½Ρ ΠΌΠ΅ΡΡΡΠ° (Π»ΡΠ±ΠΎΠ³ΠΎ)
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE TO_CHAR (hire_date, 'DD') = '01';
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΊΠΎΡΠΎΡΡΠ΅ ΠΏΡΠΈΡΠ»ΠΈ Π½Π° ΡΠ°Π±ΠΎΡΡ Π² 2008ΠΎΠΌ Π³ΠΎΠ΄Ρ
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE TO_CHAR (hire_date, 'YYYY') = '2008';
Π’Π°Π±Π»ΠΈΡΠ° DUAL. ΠΠΎΠΊΠ°Π·Π°ΡΡ Π·Π°Π²ΡΡΠ°ΡΠ½ΡΡ Π΄Π°ΡΡ Π² ΡΠΎΡΠΌΠ°ΡΠ΅: Tomorrow is Second day of January
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT TO_CHAR (SYSDATE, 'fm""Tomorrow is ""Ddspth ""day of"" Month') info
FROM DUAL;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΈ Π΄Π°ΡΡ ΠΏΡΠΈΡ
ΠΎΠ΄Π° Π½Π° ΡΠ°Π±ΠΎΡΡ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ Π² ΡΠΎΡΠΌΠ°ΡΠ΅: 21st of June, 2007
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT first_name, TO_CHAR (hire_date, 'fmddth ""of"" Month, YYYY') hire_date
FROM employees;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ ΡΠ°Π±ΠΎΡΠ½ΠΈΠΊΠΎΠ² Ρ ΡΠ²Π΅Π»ΠΈΡΠ΅Π½Π½ΡΠΌΠΈ Π·Π°ΡΠΏΠ»Π°ΡΠ°ΠΌΠΈ Π½Π° 20%. ΠΠ°ΡΠΏΠ»Π°ΡΡ ΠΏΠΎΠΊΠ°Π·Π°ΡΡ ΡΠΎ Π·Π½Π°ΠΊΠΎΠΌ Π΄ΠΎΠ»Π»Π°ΡΠ°
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT first_name, TO_CHAR (salary + salary * 0.20, 'fm$999,999.00') new_salary
FROM employees;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΊΠΎΡΠΎΡΡΠ΅ ΠΏΡΠΈΡΠΈ Π½Π° ΡΠ°Π±ΠΎΡΡ Π² ΡΠ΅Π²ΡΠ°Π»Π΅ 2007Π³ΠΎ Π³ΠΎΠ΄Π°.
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE hire_date BETWEEN TO_DATE ('01.02.2007', 'DD.MM.YYYY')
AND LAST_DAY (TO_DATE ('01.02.2007', 'DD.MM.YYYY'));
SELECT *
FROM employees
WHERE to_char(hire_date,'MM.YYYY') = '02.2007';
Π’Π°Π±Π»ΠΈΡΠ° DUAL. ΠΡΠ²Π΅Π·ΡΠΈ Π°ΠΊΡΡΠ°Π»ΡΠ½ΡΡ Π΄Π°ΡΡ, + ΡΠ΅ΠΊΡΠ½Π΄Π°, + ΠΌΠΈΠ½ΡΡΠ°, + ΡΠ°Ρ, + Π΄Π΅Π½Ρ, + ΠΌΠ΅ΡΡΡ, + Π³ΠΎΠ΄
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT SYSDATE now,
SYSDATE + 1 / (24 * 60 * 60) plus_second,
SYSDATE + 1 / (24 * 60) plus_minute,
SYSDATE + 1 / 24 plus_hour,
SYSDATE + 1 plus_day,
ADD_MONTHS (SYSDATE, 1) plus_month,
ADD_MONTHS (SYSDATE, 12) plus_year
FROM DUAL;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Ρ ΠΏΠΎΠ»Π½ΡΠΌΠΈ Π·Π°ΡΠΏΠ»Π°ΡΠ°ΠΌΠΈ (salary + commission_pct(%)) Π² ΡΠΎΡΠΌΠ°ΡΠ΅: $24,000.00
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT first_name, salary, TO_CHAR (salary + salary * NVL (commission_pct, 0), 'fm$99,999.00') full_salary
FROM employees;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΈ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΎ Π½Π°Π»ΠΈΡΠΈΠΈ Π±ΠΎΠ½ΡΡΠΎΠ² ΠΊ Π·Π°ΡΠΏΠ»Π°ΡΠ΅ (Yes/No)
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT first_name, commission_pct, NVL2 (commission_pct, 'Yes', 'No') has_bonus
FROM employees;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΡΠΎΠ²Π΅Π½Ρ Π·Π°ΡΠΏΠ»Π°ΡΡ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠ°: ΠΠ΅Π½ΡΡΠ΅ 5000 ΡΡΠΈΡΠ°Π΅ΡΡΡ Low level, ΠΠΎΠ»ΡΡΠ΅ ΠΈΠ»ΠΈ ΡΠ°Π²Π½ΠΎ 5000 ΠΈ ΠΌΠ΅Π½ΡΡΠ΅ 10000 ΡΡΠΈΡΠ°Π΅ΡΡΡ Normal level, ΠΠΎΠ»ΡΡΠ΅ ΠΈΠΎΠΈ ΡΠ°Π²Π½ΠΎ 10000 ΡΡΠΈΡΠ°Π΅ΡΡΡ High level
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT first_name,
salary,
CASE
WHEN salary < 5000 THEN 'Low'
WHEN salary >= 5000 AND salary < 10000 THEN 'Normal'
ELSE 'High'
END salary_level
FROM employees;
Π’Π°Π±Π»ΠΈΡΠ° Countries. ΠΠ»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΡΡΡΠ°Π½Ρ ΠΏΠΎΠΊΠ°Π·Π°ΡΡ ΡΠ΅Π³ΠΈΠΎΠ½ Π² ΠΊΠΎΡΠΎΡΠΎΠΌ ΠΎΠ½ Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ: 1-Europe, 2-America, 3-Asia, 4-Africa (Π±Π΅Π· Join)
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT country_name country,
DECODE (region_id,
1, 'Europe',
2, 'America',
3, 'Asia',
4, 'Africa',
'Unknown')
region
FROM countries;
SELECT country_name
country,
CASE region_id
WHEN 1 THEN 'Europe'
WHEN 2 THEN 'America'
WHEN 3 THEN 'Asia'
WHEN 4 THEN 'Africa'
ELSE 'Unknown'
END
region
FROM countries;
Reporting Aggregated Data Using the Group Functions
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠ΅ΠΏΠΎΡΡ ΠΏΠΎ department_id Ρ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠΉ ΠΈ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠΉ Π·Π°ΡΠΏΠ»Π°ΡΠΎΠΉ, Ρ ΡΠ°Π½Π½Π΅ΠΉ ΠΈ ΠΏΠΎΠ·Π΄Π½Π΅ΠΉ Π΄Π°ΡΠΎΠΉ ΠΏΡΠΈΡ
ΠΎΠ΄Π° Π½Π° ΡΠ°Π±ΠΎΡΡ ΠΈ Ρ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎΠ² ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ². Π‘ΠΎΡΠΈΡΠΎΠ²Π°ΡΡ ΠΏΠΎ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²Ρ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² (ΠΏΠΎ ΡΠ±ΡΠ²Π°Π½ΠΈΡ)
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT department_id,
MIN (salary) min_salary,
MAX (salary) max_salary,
MIN (hire_date) min_hire_date,
MAX (hire_date) max_hire_Date,
COUNT (*) count
FROM employees
GROUP BY department_id
order by count(*) desc;
Π’Π°Π±Π»ΠΈΡΠ° Employees. Π‘ΠΊΠΎΠ»ΡΠΊΠΎ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΈΠΌΠ΅Π½Π° ΠΊΠΎΡΠΎΡΡΡ
Π½Π°ΡΠΈΠ½Π°Π΅ΡΡΡ Ρ ΠΎΠ΄Π½ΠΎΠΉ ΠΈ ΡΠΎΠΉ ΠΆΠ΅ Π±ΡΠΊΠ²Ρ? Π‘ΠΎΡΡΠΈΡΠΎΠ²Π°ΡΡ ΠΏΠΎ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²Ρ. ΠΠΎΠΊΠ°Π·ΡΠ²Π°ΡΡ ΡΠΎΠ»ΡΠΊΠΎ ΡΠ΅ Π³Π΄Π΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π±ΠΎΠ»ΡΡΠ΅ 1
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT SUBSTR (first_name, 1, 1) first_char, COUNT (*)
FROM employees
GROUP BY SUBSTR (first_name, 1, 1)
HAVING COUNT (*) > 1
ORDER BY 2 DESC;
Π’Π°Π±Π»ΠΈΡΠ° Employees. Π‘ΠΊΠΎΠ»ΡΠΊΠΎ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠ°Π±ΠΎΡΠ°ΡΡ Π² ΠΎΠ΄Π½ΠΎΠΌ ΠΈ ΡΠΎΠΆΠ΅ ΠΎΡΠ΄Π΅Π»Π΅ ΠΈ ΠΏΠΎΠ»ΡΡΠ°ΡΡ ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²ΡΡ Π·Π°ΡΠΏΠ»Π°ΡΡ?
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT department_id, salary, COUNT (*)
FROM employees
GROUP BY department_id, salary
HAVING COUNT (*) > 1;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠ΅ΠΏΠΎΡΡ ΡΠΊΠΎΠ»ΡΠΊΠΎ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΏΡΠΈΠ½ΡΠ»ΠΈ Π½Π° ΡΠ°Π±ΠΎΡΡ Π² ΠΊΠ°ΠΆΠ΄ΡΠΉ Π΄Π΅Π½Ρ Π½Π΅Π΄Π΅Π»ΠΈ. Π‘ΠΎΡΡΠΈΡΠΎΠ²Π°ΡΡ ΠΏΠΎ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²Ρ
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT TO_CHAR (hire_Date, 'Day') day, COUNT (*)
FROM employees
GROUP BY TO_CHAR (hire_Date, 'Day')
ORDER BY 2 DESC;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠ΅ΠΏΠΎΡΡ ΡΠΊΠΎΠ»ΡΠΊΠΎ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΏΡΠΈΠ½ΡΠ»ΠΈ Π½Π° ΡΠ°Π±ΠΎΡΡ ΠΏΠΎ Π³ΠΎΠ΄Π°ΠΌ. Π‘ΠΎΡΡΠΈΡΠΎΠ²Π°ΡΡ ΠΏΠΎ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²Ρ
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT TO_CHAR (hire_date, 'YYYY') year, COUNT (*)
FROM employees
GROUP BY TO_CHAR (hire_date, 'YYYY');
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π΄Π΅ΠΏΠ°ΡΡΠ°ΠΌΠ΅Π½ΡΠΎΠ² Π² ΠΊΠΎΡΠΎΡΠΎΠΌ Π΅ΡΡΡ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΈ
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT COUNT (COUNT (*)) department_count
FROM employees
WHERE department_id IS NOT NULL
GROUP BY department_id;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ department_id Π² ΠΊΠΎΡΠΎΡΠΎΠΌ ΡΠ°Π±ΠΎΡΠ°ΡΡ Π±ΠΎΠ»ΡΡΠ΅ 30 ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ²
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT department_id
FROM employees
GROUP BY department_id
HAVING COUNT (*) > 30;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ department_id ΠΈ ΠΎΠΊΡΡΠ³Π»Π΅Π½Π½ΡΡ ΡΡΠ΅Π΄Π½ΡΡ Π·Π°ΡΠΏΠ»Π°ΡΡ ΡΠ°Π±ΠΎΡΠ½ΠΈΠΊΠΎΠ² Π² ΠΊΠ°ΠΆΠ΄ΠΎΠΌ Π΄Π΅ΠΏΠ°ΡΡΠ°ΠΌΠ΅Π½ΡΠ΅.
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT department_id, ROUND (AVG (salary)) avg_salary
FROM employees
GROUP BY department_id;
Π’Π°Π±Π»ΠΈΡΠ° Countries. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ region_id ΡΡΠΌΠΌΠ° Π²ΡΠ΅Ρ
Π±ΡΠΊΠ² Π²ΡΠ΅Ρ
country_name Π² ΠΊΠΎΡΠΎΡΠΎΠΌ Π±ΠΎΠ»ΡΡΠ΅ 60ΡΠΈ
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT region_id
FROM countries
GROUP BY region_id
HAVING SUM (LENGTH (country_name)) > 60;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ department_id Π² ΠΊΠΎΡΠΎΡΠΎΠΌ ΡΠ°Π±ΠΎΡΠ°ΡΡ ΡΠ°Π±ΠΎΡΠ½ΠΈΠΊΠΈ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΈΡ
(>1) job_id
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT department_id
FROM employees
GROUP BY department_id
HAVING COUNT (DISTINCT job_id) > 1;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ manager_id Ρ ΠΊΠΎΡΠΎΡΡΡ
ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΏΠΎΠ΄ΡΠΈΠ½Π΅Π½Π½ΡΡ
Π±ΠΎΠ»ΡΡΠ΅ 5 ΠΈ ΡΡΠΌΠΌΠ° Π²ΡΠ΅Ρ
Π·Π°ΡΠΏΠ»Π°Ρ Π΅Π³ΠΎ ΠΏΠΎΠ΄ΡΠΈΠ½Π΅Π½Π½ΡΡ
Π±ΠΎΠ»ΡΡΠ΅ 50000
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT manager_id
FROM employees
GROUP BY manager_id
HAVING COUNT (*) > 5 AND SUM (salary) > 50000;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ manager_id Ρ ΠΊΠΎΡΠΎΡΡΡ
ΡΡΠ΅Π΄Π½ΡΡ Π·Π°ΡΠΏΠ»Π°ΡΠ° Π²ΡΠ΅Ρ
Π΅Π³ΠΎ ΠΏΠΎΠ΄ΡΠΈΠ½Π΅Π½Π½ΡΡ
Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ Π² ΠΏΡΠΎΠΌΠ΅ΠΆΡΡΠΊΠ΅ ΠΎΡ 6000 Π΄ΠΎ 9000 ΠΊΠΎΡΠΎΡΡΠ΅ Π½Π΅ ΠΏΠΎΠ»ΡΡΠ°ΡΡ Π±ΠΎΠ½ΡΡΡ (commission_pct ΠΏΡΡΡΠΎΠΉ)
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT manager_id, AVG (salary) avg_salary
FROM employees
WHERE commission_pct IS NULL
GROUP BY manager_id
HAVING AVG (salary) BETWEEN 6000 AND 9000;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΡΡ Π·Π°ΡΠΏΠ»Π°ΡΡ ΠΈΠ· Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² job_id ΠΊΠΎΡΠΎΡΡΠ· Π·Π°ΠΊΠ°Π½ΡΠΈΠ²Π°Π΅ΡΡΡ Π½Π° ΡΠ»ΠΎΠ²ΠΎ ‘CLERK’
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT MAX (salary) max_salary
FROM employees
WHERE job_id LIKE '%CLERK';
SELECT MAX (salary) max_salary
FROM employees
WHERE SUBSTR (job_id, -5) = 'CLERK';
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΡΡ Π·Π°ΡΠΏΠ»Π°ΡΡ ΡΡΠ΅Π΄ΠΈ Π²ΡΠ΅Ρ
ΡΡΠ΅Π΄Π½ΠΈΡ
Π·Π°ΡΠΏΠ»Π°Ρ ΠΏΠΎ Π΄Π΅ΠΏΠ°ΡΡΠ°ΠΌΠ΅Π½ΡΡ
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT MAX (AVG (salary))
FROM employees
GROUP BY department_id;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Ρ ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²ΡΠΌ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎΠΌ Π±ΡΠΊΠ² Π² ΠΈΠΌΠ΅Π½ΠΈ. ΠΡΠΈ ΡΡΠΎΠΌ ΠΏΠΎΠΊΠ°Π·Π°ΡΡ ΡΠΎΠ»ΡΠΊΠΎ ΡΠ΅Ρ
Ρ ΠΊΠΎΠ³ΠΎ Π΄Π»ΠΈΠ½Π° ΠΈΠΌΠ΅Π½ΠΈ Π±ΠΎΠ»ΡΡΠ΅ 5 ΠΈ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Ρ ΡΠ°ΠΊΠΈΠΌ ΠΈΠΌΠ΅Π½Π΅ΠΌ Π±ΠΎΠ»ΡΡΠ΅ 20. Π‘ΠΎΡΡΠΈΡΠΎΠ²Π°ΡΡ ΠΏΠΎ Π΄Π»ΠΈΠ½Π½Π΅ ΠΈΠΌΠ΅Π½ΠΈ
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT LENGTH (first_name), COUNT (*)
FROM employees
GROUP BY LENGTH (first_name)
HAVING LENGTH (first_name) > 5 AND COUNT (*) > 20
ORDER BY LENGTH (first_name);
SELECT LENGTH (first_name), COUNT (*)
FROM employees
WHERE LENGTH (first_name) > 5
GROUP BY LENGTH (first_name)
HAVING COUNT (*) > 20
ORDER BY LENGTH (first_name);
Displaying Data from Multiple Tables Using Joins
Π’Π°Π±Π»ΠΈΡΠ° Employees, Departaments, Locations, Countries, Regions. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ ΡΠ΅Π³ΠΈΠΎΠ½ΠΎΠ² ΠΈ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Π² ΠΊΠ°ΠΆΠ΄ΠΎΠΌ ΡΠ΅Π³ΠΈΠΎΠ½Π΅
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT region_name, COUNT (*)
FROM employees e
JOIN departments d ON (e.department_id = d.department_id)
JOIN locations l ON (d.location_id = l.location_id)
JOIN countries c ON (l.country_id = c.country_id)
JOIN regions r ON (c.region_id = r.region_id)
GROUP BY region_name;
Π’Π°Π±Π»ΠΈΡΠ° Employees, Departaments, Locations, Countries, Regions. ΠΠΎΠ»ΡΡΠΈΡΡ Π΄Π΅ΡΠ°Π»ΡΠ½ΡΡ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΎ ΠΊΠ°ΠΆΠ΄ΠΎΠΌ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠ΅:
First_name, Last_name, Departament, Job, Street, Country, Region
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT First_name,
Last_name,
Department_name,
Job_id,
street_address,
Country_name,
Region_name
FROM employees e
JOIN departments d ON (e.department_id = d.department_id)
JOIN locations l ON (d.location_id = l.location_id)
JOIN countries c ON (l.country_id = c.country_id)
JOIN regions r ON (c.region_id = r.region_id);
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠΊΠ°Π·Π°ΡΡ Π²ΡΠ΅Ρ
ΠΌΠ΅Π½Π΅Π΄ΠΆΠ΅ΡΠΎΠ² ΠΊΠΎΡΠΎΡΡΠ΅ ΠΈΠΌΠ΅ΡΡ Π² ΠΏΠΎΠ΄ΡΠΈΠ½Π΅Π½ΠΈΠΈ Π±ΠΎΠ»ΡΡΠ΅ 6ΡΠΈ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ²
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT man.first_name, COUNT (*)
FROM employees emp JOIN employees man ON (emp.manager_id = man.employee_id)
GROUP BY man.first_name
HAVING COUNT (*) > 6;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠΊΠ°Π·Π°ΡΡ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΊΠΎΡΠΎΡΡΠ΅ Π½ΠΈ ΠΊΠΎΠΌΡ Π½Π΅ ΠΏΠΎΠ΄ΡΠΈΠ½ΡΡΡΡΡ
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT emp.first_name
FROM employees emp
LEFT JOIN employees man ON (emp.manager_id = man.employee_id)
WHERE man.FIRST_NAME IS NULL;
SELECT first_name
FROM employees
WHERE manager_id IS NULL;
Π’Π°Π±Π»ΠΈΡΠ° Employees, Job_history. Π ΡΠ°Π±Π»ΠΈΡΠ΅ Employee Ρ
ΡΠ°Π½ΡΡΡΡ Π²ΡΠ΅ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΈ. Π ΡΠ°Π±Π»ΠΈΡΠ΅ Job_history Ρ
ΡΠ°Π½ΡΡΡΡ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΈ ΠΊΠΎΡΠΎΡΡΠ΅ ΠΏΠΎΠΊΠΈΠ½ΡΠ»ΠΈ ΠΊΠΎΠΌΠΏΠ°Π½ΠΈΡ. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠ΅ΠΏΠΎΡΡ ΠΎ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠ°Ρ
ΠΈ ΠΎ Π΅Π³ΠΎ ΡΡΠ°ΡΡΡΠ΅ Π² ΠΊΠΎΠΌΠΏΠ°Π½ΠΈΠΈ (Π Π°Π±ΠΎΡΠ°Π΅Ρ ΠΈΠ»ΠΈ ΠΏΠΎΠΊΠΈΠ½ΡΠ» ΠΊΠΎΠΌΠΏΠ°Π½ΠΈΡ Ρ Π΄Π°ΡΠΎΠΉ ΡΡ
ΠΎΠ΄Π°)
ΠΡΠΈΠΌΠ΅Ρ:
first_name | status
Jennifer | Left the company at 31 of December, 2006
Clara | Currently Working
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT first_name,
NVL2 (
end_date,
TO_CHAR (end_date, 'fm""Left the company at"" DD ""of"" Month, YYYY'),
'Currently Working')
status
FROM employees e LEFT JOIN job_history j ON (e.employee_id = j.employee_id);
Π’Π°Π±Π»ΠΈΡΠ° Employees, Departaments, Locations, Countries, Regions. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΊΠΎΡΠΎΡΡΠ΅ ΠΆΠΈΠ²ΡΡ Π² Europe (region_name)
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT first_name
FROM employees
JOIN departments USING (department_id)
JOIN locations USING (location_id)
JOIN countries USING (country_id)
JOIN regions USING (region_id)
WHERE region_name = 'Europe';
SELECT first_name
FROM employees e
JOIN departments d ON (e.department_id = d.department_id)
JOIN locations l ON (d.location_id = l.location_id)
JOIN countries c ON (l.country_id = c.country_id)
JOIN regions r ON (c.region_id = r.region_id)
WHERE region_name = 'Europe';
Π’Π°Π±Π»ΠΈΡΠ° Employees, Departaments. ΠΠΎΠΊΠ°Π·Π°ΡΡ Π²ΡΠ΅ Π΄Π΅ΠΏΠ°ΡΡΠ°ΠΌΠ΅Π½ΡΡ Π² ΠΊΠΎΡΠΎΡΡΡ
ΡΠ°Π±ΠΎΡΠ°ΡΡ Π±ΠΎΠ»ΡΡΠ΅ 30ΡΠΈ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ²
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT department_name, COUNT (*)
FROM employees e JOIN departments d ON (e.department_id = d.department_id)
GROUP BY department_name
HAVING COUNT (*) > 30;
Π’Π°Π±Π»ΠΈΡΠ° Employees, Departaments. ΠΠΎΠΊΠ°Π·Π°ΡΡ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΊΠΎΡΠΎΡΡΠ΅ Π½Π΅ ΡΠΎΡΡΠΎΡΡ Π½ΠΈ Π² ΠΎΠ΄Π½ΠΎΠΌ Π΄Π΅ΠΏΠ°ΡΡΠ°ΠΌΠ΅Π½ΡΠ΅
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT first_name
FROM employees e
LEFT JOIN departments d ON (e.department_id = d.department_id)
WHERE d.department_name IS NULL;
SELECT first_name
FROM employees
WHERE department_id IS NULL;
Π’Π°Π±Π»ΠΈΡΠ° Employees, Departaments. ΠΠΎΠΊΠ°Π·Π°ΡΡ Π²ΡΠ΅ Π΄Π΅ΠΏΠ°ΡΡΠ°ΠΌΠ΅Π½ΡΡ Π² ΠΊΠΎΡΠΎΡΡΡ
Π½Π΅Ρ Π½ΠΈ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠ°
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT department_name
FROM employees e
RIGHT JOIN departments d ON (e.department_id = d.department_id)
WHERE first_name IS NULL;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠΊΠ°Π·Π°ΡΡ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Ρ ΠΊΠΎΡΠΎΡΡΡ
Π½Π΅Ρ Π½ΠΈ ΠΊΠΎΠ³ΠΎ Π² ΠΏΠΎΠ΄ΡΠΈΠ½Π΅Π½ΠΈΠΈ
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT man.first_name
FROM employees emp
RIGHT JOIN employees man ON (emp.manager_id = man.employee_id)
WHERE emp.FIRST_NAME IS NULL;
Π’Π°Π±Π»ΠΈΡΠ° Employees, Jobs, Departaments. ΠΠΎΠΊΠ°Π·Π°ΡΡ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Π² ΡΠΎΡΠΌΠ°ΡΠ΅: First_name, Job_title, Department_name.
ΠΡΠΈΠΌΠ΅Ρ:
First_name | Job_title | Department_name
Donald | Shipping | Clerk Shipping
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT first_name, job_title, department_name
FROM employees e
JOIN jobs j ON (e.job_id = j.job_id)
JOIN departments d ON (d.department_id = e.department_id);
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΌΠ΅Π½Π΅Π΄ΠΆΠ΅ΡΡ ΠΊΠΎΡΠΎΡΡΡ
ΡΡΡΡΠΎΠΈΠ»ΠΈΡΡ Π½Π° ΡΠ°Π±ΠΎΡΡ Π² 2005ΠΎΠΌ Π³ΠΎΠ΄Ρ Π½ΠΎ ΠΏΡΠΈ ΡΡΠΎ ΡΠ°ΠΌΠΈ ΡΡΠΈ ΡΠ°Π±ΠΎΡΠ½ΠΈΠΊΠΈ ΡΡΡΡΠΎΠΈΠ»ΠΈΡΡ Π½Π° ΡΠ°Π±ΠΎΡΡ Π΄ΠΎ 2005 Π³ΠΎΠ΄Π°
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT emp.*
FROM employees emp JOIN employees man ON (emp.manager_id = man.employee_id)
WHERE TO_CHAR (man.hire_date, 'YYYY') = '2005'
AND emp.hire_date < TO_DATE ('01012005', 'DDMMYYYY');
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΌΠ΅Π½Π΅Π΄ΠΆΠ΅ΡΡ ΠΊΠΎΡΠΎΡΡΡ
ΡΡΡΡΠΎΠΈΠ»ΠΈΡΡ Π½Π° ΡΠ°Π±ΠΎΡΡ Π² ΡΠ½Π²Π°ΡΠ΅ ΠΌΠ΅ΡΡΡΠ΅ Π»ΡΠ±ΠΎΠ³ΠΎ Π³ΠΎΠ΄Π° ΠΈ Π΄Π»ΠΈΠ½Π° job_title ΡΡΠΈΡ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Π±ΠΎΠ»ΡΡΠ΅ 15ΡΠΈ ΡΠΈΠΌΠ²ΠΎΠ»ΠΎΠ²
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT emp.*
FROM employees emp
JOIN employees man ON (emp.manager_id = man.employee_id)
JOIN jobs j ON (emp.job_id = j.job_id)
WHERE TO_CHAR (man.hire_date, 'MM') = '01' AND LENGTH (j.job_title) > 15;
Using Subqueries to Solve Queries
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Ρ ΡΠ°ΠΌΡΠΌ Π΄Π»ΠΈΠ½Π½ΡΠΌ ΠΈΠΌΠ΅Π½Π΅ΠΌ.
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE LENGTH (first_name) =
(SELECT MAX (LENGTH (first_name)) FROM employees);
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Ρ Π·Π°ΡΠΏΠ»Π°ΡΠΎΠΉ Π±ΠΎΠ»ΡΡΠ΅ΠΉ ΡΡΠ΅Π΄Π½Π΅ΠΉ Π·Π°ΡΠΏΠ»Π°ΡΡ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ².
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE salary > (SELECT AVG (salary) FROM employees);
Π’Π°Π±Π»ΠΈΡΠ° Employees, Departments, Locations. ΠΠΎΠ»ΡΡΠΈΡΡ Π³ΠΎΡΠΎΠ΄ Π² ΠΊΠΎΡΠΎΡΠΎΠΌ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΈ Π² ΡΡΠΌΠΌΠ΅ Π·Π°ΡΠ°Π±Π°ΡΡΠ²Π°ΡΡ ΠΌΠ΅Π½ΡΡΠ΅ Π²ΡΠ΅Ρ
.
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT city
FROM employees e
JOIN departments d ON (e.department_id = d.department_id)
JOIN locations l ON (d.location_id = l.location_id)
GROUP BY city
HAVING SUM (salary) =
( SELECT MIN (SUM (salary))
FROM employees e
JOIN departments d ON (e.department_id = d.department_id)
JOIN locations l ON (d.location_id = l.location_id)
GROUP BY city);
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Ρ ΠΊΠΎΡΠΎΡΡΡ
ΠΌΠ΅Π½Π΅Π΄ΠΆΠ΅Ρ ΠΏΠΎΠ»ΡΡΠ°Π΅Ρ Π·Π°ΡΠΏΠ»Π°ΡΡ Π±ΠΎΠ»ΡΡΠ΅ 15000.
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE manager_id IN (SELECT employee_id
FROM employees
WHERE salary > 15000)
Π’Π°Π±Π»ΠΈΡΠ° Employees, Departaments. ΠΠΎΠΊΠ°Π·Π°ΡΡ Π²ΡΠ΅ Π΄Π΅ΠΏΠ°ΡΡΠ°ΠΌΠ΅Π½ΡΡ Π² ΠΊΠΎΡΠΎΡΡΡ
Π½Π΅Ρ Π½ΠΈ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠ°
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM departments
WHERE department_id NOT IN (SELECT department_id
FROM employees
WHERE department_id IS NOT NULL);
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠΊΠ°Π·Π°ΡΡ Π²ΡΠ΅Ρ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΊΠΎΡΠΎΡΡΠ΅ Π½Π΅ ΡΠ²Π»ΡΡΡΡΡ ΠΌΠ΅Π½Π΅Π΄ΠΆΠ΅ΡΠ°ΠΌΠΈ
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE employee_id NOT IN (SELECT manager_id
FROM employees
WHERE manager_id IS NOT NULL)
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠΊΠ°Π·Π°ΡΡ Π²ΡΠ΅Ρ
ΠΌΠ΅Π½Π΅Π΄ΠΆΠ΅ΡΠΎΠ² ΠΊΠΎΡΠΎΡΡΠ΅ ΠΈΠΌΠ΅ΡΡ Π² ΠΏΠΎΠ΄ΡΠΈΠ½Π΅Π½ΠΈΠΈ Π±ΠΎΠ»ΡΡΠ΅ 6ΡΠΈ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ²
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees e
WHERE (SELECT COUNT (*)
FROM employees
WHERE manager_id = e.employee_id) > 6;
Π’Π°Π±Π»ΠΈΡΠ° Employees, Departaments. ΠΠΎΠΊΠ°Π·Π°ΡΡ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠ°Π±ΠΎΡΠ°ΡΡ Π² Π΄Π΅ΠΏΠ°ΡΡΠ°ΠΌΠ΅Π½ΡΠ΅ IT
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE department_id = (SELECT department_id
FROM departments
WHERE department_name = 'IT');
Π’Π°Π±Π»ΠΈΡΠ° Employees, Jobs, Departaments. ΠΠΎΠΊΠ°Π·Π°ΡΡ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Π² ΡΠΎΡΠΌΠ°ΡΠ΅: First_name, Job_title, Department_name.
ΠΡΠΈΠΌΠ΅Ρ:
First_name | Job_title | Department_name
Donald | Shipping | Clerk Shipping
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT first_name,
(SELECT job_title
FROM jobs
WHERE job_id = e.job_id)
job_title,
(SELECT department_name
FROM departments
WHERE department_id = e.department_id)
department_name
FROM employees e;
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΌΠ΅Π½Π΅Π΄ΠΆΠ΅ΡΡ ΠΊΠΎΡΠΎΡΡΡ
ΡΡΡΡΠΎΠΈΠ»ΠΈΡΡ Π½Π° ΡΠ°Π±ΠΎΡΡ Π² 2005ΠΎΠΌ Π³ΠΎΠ΄Ρ Π½ΠΎ ΠΏΡΠΈ ΡΡΠΎ ΡΠ°ΠΌΠΈ ΡΡΠΈ ΡΠ°Π±ΠΎΡΠ½ΠΈΠΊΠΈ ΡΡΡΡΠΎΠΈΠ»ΠΈΡΡ Π½Π° ΡΠ°Π±ΠΎΡΡ Π΄ΠΎ 2005 Π³ΠΎΠ΄Π°
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees
WHERE manager_id IN (SELECT employee_id
FROM employees
WHERE TO_CHAR (hire_date, 'YYYY') = '2005')
AND hire_date < TO_DATE ('01012005', 'DDMMYYYY');
Π’Π°Π±Π»ΠΈΡΠ° Employees. ΠΠΎΠ»ΡΡΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² ΠΌΠ΅Π½Π΅Π΄ΠΆΠ΅ΡΡ ΠΊΠΎΡΠΎΡΡΡ
ΡΡΡΡΠΎΠΈΠ»ΠΈΡΡ Π½Π° ΡΠ°Π±ΠΎΡΡ Π² ΡΠ½Π²Π°ΡΠ΅ ΠΌΠ΅ΡΡΡΠ΅ Π»ΡΠ±ΠΎΠ³ΠΎ Π³ΠΎΠ΄Π° ΠΈ Π΄Π»ΠΈΠ½Π° job_title ΡΡΠΈΡ
ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠ² Π±ΠΎΠ»ΡΡΠ΅ 15ΡΠΈ ΡΠΈΠΌΠ²ΠΎΠ»ΠΎΠ²
Π Π΅ΡΠ΅Π½ΠΈΠ΅
SELECT *
FROM employees e
WHERE manager_id IN (SELECT employee_id
FROM employees
WHERE TO_CHAR (hire_date, 'MM') = '01')
AND (SELECT LENGTH (job_title)
FROM jobs
WHERE job_id = e.job_id) > 15;
ΠΠ° ΡΡΠΎΠΌ ΠΏΠΎΠΊΠ° Π²ΡΡ.
ΠΠ°Π΄Π΅ΡΡΡ, Π·Π°Π΄Π°ΡΠΈ Π±ΡΠ»ΠΈ ΠΈΠ½ΡΠ΅ΡΠ΅ΡΠ½ΡΠΌΠΈ ΠΈ ΡΠ²Π»Π΅ΠΊΠ°ΡΠ΅Π»ΡΠ½ΡΠΌΠΈ.
ΠΡΠ΄Ρ ΠΏΠΎ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΡΡΠΈ Π΄ΠΎΠΏΠΎΠ»Π½ΡΡΡ ΡΡΠΎΡ ΡΠΏΠΈΡΠΎΠΊ Π·Π°Π΄Π°Ρ.
Π’Π°ΠΊΠΆΠ΅ Π±ΡΠ΄Ρ ΡΠ°Π΄ Π»ΡΠ±ΡΠΌ Π·Π°ΠΌΠ΅ΡΠ°Π½ΠΈΡΠΌ ΠΈ ΠΏΡΠ΅Π΄Π»ΠΎΠΆΠ΅Π½ΠΈΡΠΌ.
P.S.: ΠΡΠ»ΠΈ ΠΊΠΎΠΌΡ ΡΠΎ Π² Π³ΠΎΠ»ΠΎΠ²Ρ ΠΏΡΠΈΠ΄Π΅Ρ ΠΈΠ½ΡΠ΅ΡΠ΅ΡΠ½Π°Ρ Π·Π°Π΄Π°ΡΠ° Π½Π° SELECT, ΠΏΠΈΡΠΈΡΠ΅ Π² ΠΊΠΎΠΌΠ΅Π½ΡΠ°ΡΠΈΡΡ , Π΄ΠΎΠ±Π°Π²Π»Ρ Π² ΡΠΏΠΈΡΠΎΠΊ.
Π‘ΠΏΠ°ΡΠΈΠ±ΠΎ.
ΠΡΡΠΎΡΠ½ΠΈΠΊ: habr.com