Lesson 3 - SQL Advance
Lesson 3 - SQL Advance
<Trainer-Name>
Syntax:
CASE biểu_thức
WHEN biểu_thức_kiểm_tra THEN kết_quả
[ ... ]
[ELSE kết_quả_của_else]
END
SELECT masv,hodem,ten,
CASE gioitinh
WHEN 1 THEN 'Nam'
ELSE 'Nữ'
END AS gioitinh
FROM sinhvien
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4
Advanced DML Statements
Joins
Cross Join
Cartesian product. Simply merges two tables.
Self Join
Joining a table with itself
Inner Join
Cross join with a condition. Used to find matching
records in the two tables
Outer Join
Used to find un matched rows in the two tables
Join types: left joint, right join, full join
If I put
Select A.Id, B.Id from A,B
This generates output as
A1
B1
C1
A2
B2
C2
If I want to print all managers using self join, I should write query as:
Example:
I have 2 tables Student(sid, Name) and Marks(Sid, Subject, Score)
If I want to print the marks of all students in the following format,
Name Subject Score
Employee
Eid EName Mid
1 ABC 1
2 DEF 3
Machine
Mid ManufacturerName
1 Zenith
2 HP
If I write a query using inner join, then the second employee will
not be displayed as the mid in his record is not avilable with the second
table.
Machine
Mid ManufacturerName
1 Zenith
2 HP
If I want to find which machine is unallocated, I can use right outer join.
If I want to find people who have been un allocated with a system and
machines that are been un allocated, I can go for full outer join.
SELECT SNAME
FROM S
WHERE SNO IN
(SELECT SNO
FROM SP
WHERE PNO =‘P2’)
SELECT SNO
FROM S
WHERE STATUS <
(SELECT STATUS
FROM S
WHERE SNO=‘S1’)
SELECT SNO
FROM S
WHERE STATUS <
(SELECT MAX(STATUS)
FROM S)
Used in sub-query
SELECT hodem,ten
FROM sinhvien JOIN lop on sinhvien.malop=lop.malop
WHERE tenlop='Tin K25' AND
year(ngaysinh)IN(SELECT year(ngaysinh)
FROM sinhvien JOIN lop
ON sinhvien.malop=lop.malop
WHERE lop.tenlop='Toán K25')
SELECT hodem,ten
FROM sinhvien
WHERE NOT EXISTS(SELECT masv FROM diemthi
WHERE diemthi.masv=sinhvien.masv)
Using:
SELECT name FROM employee
WHERE (salary, age) = (SELECT MAX (salary), MAX (age)
FROM employee_details) AND dept = 'Electronics';
Instead of:
SELECT name FROM employee
WHERE salary = (SELECT MAX(salary) FROM employee_details)
AND age = (SELECT MAX(age) FROM employee_details)
AND emp_dept = 'Electronics';
Using:
Select * from product p
where EXISTS (select * from order_items
o
where o.product_id = p.product_id)
Instead of:
Select * from product p
where product_id IN
(select product_id from order_items )
Using:
SELECT product_id, product_name
FROM product
WHERE unit_price BETWEEN MAX(unit_price)
and MIN(unit_price)
Instead of:
SELECT product_id, product_name
FROM product
WHERE unit_price >= MAX(unit_price)
and unit_price <= MIN(unit_price)
Nhất thiết phải chỉ định item trong câu lệnh INSERT.
Table-A
Key1
CHAR(1)
Col1
CHAR(1)
① INSERT
Col2 INTO Table-A VALUES ( ‘1’ ,‘A’,‘X’ )
② INSERT
CHAR(1) INTO Table-A ( Key1,Col1,Col2 )
VALUES ( ‘1’ ,‘A’,‘X’ )
© ©FPT
FPTSOFTWARE
SOFTWARE– –TRAINING 41 – Internal use
TRAINING MATERIAL
MATERIAL 04e-BM/DT/HDCV/FSOFT
04e-BM/NS/HDCV/FSOFT v2/4
v2.2
SQL Optimization Tips 12.2/12
Ví dụ sử dụng mệnh đề ON
SQL không dùng mệnh đề ON SQL có sử dùng mệnh đề ON
Bảng thực hiện JOIN và các điều kiện Bảng thực hiện JOIN và các điều kiện
của nó bị tách ra nên khó đọc câu lệnh của nó ở cùng một chỗ thì sẽ dễ đọc câu
SQL . lệnh SQL.
© ©FPT
FPTSOFTWARE
SOFTWARE– –TRAINING 42 – Internal use
TRAINING MATERIAL
MATERIAL 04e-BM/DT/HDCV/FSOFT
04e-BM/NS/HDCV/FSOFT v2/4
v2.2
Q&A
Q&A