0% found this document useful (0 votes)
11 views

SQL06 Subquery

The document discusses SQL subqueries, which are queries nested inside other SQL statements. It provides examples of different types of subqueries that can be used with SELECT, INSERT, UPDATE, and DELETE statements. While subqueries are simple and flexible, joins are generally faster. The document concludes that subqueries offer more flexibility but have lower performance than joins.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

SQL06 Subquery

The document discusses SQL subqueries, which are queries nested inside other SQL statements. It provides examples of different types of subqueries that can be used with SELECT, INSERT, UPDATE, and DELETE statements. While subqueries are simple and flexible, joins are generally faster. The document concludes that subqueries offer more flexibility but have lower performance than joins.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Quản trị CSDL SQL Server

#7. SUBQUERY – Truy vấn con


Lương Trần Hy Hiến
Sub-Queries should only be used as a fallback
solution when you cannot use a JOIN operation to
achieve the above
Conclusion on SQL Sub-Queries
• Sub queries are simple & easy to understand. It can
be easily broken down into logical steps, so it offers
more flexibility.
• The Sub queries are used in conjunction with
SELECT, INSERT, UPDATE & DELETE commands.
• In this article we have learnt about three types of
SQL supb queries: scalar, row and table sub queries.
• In SQL server, The Nested query can be used up to
32 levels.
• As compare with Joins, the performance of Sub
query is low. Joins are 500 times faster than Sub
queries.
Sub query với câu lệnh SELECT
SELECT column_name [, column_name ]
FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE])
Ví dụ:
SELECT * FROM HangHoa
WHERE MaHH IN (SELECT MaHH
FROM HangHoa
WHERE DonGia > 4500) ;
SELECT *
FROM HangHoa
WHERE MaHH NOT IN
(SELECT DISTINCT MaHH
FROM ChiTietHD);

SELECT *
FROM HangHoa P
WHERE NOT EXISTS
(SELECT NULL
FROM ChiTietHD F
WHERE P.MaHH = F.MaHH);
Sub query trong mệnh đề FROM
SELECT column_name [, column_name ]
FROM table1, (
SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE]
) as M

Ví dụ:
SELECT P.*
FROM HangHoa P,
(SELECT MAX(DonGia) DonGiaLonNhat
FROM HangHoa) M
WHERE P.DonGia = M.DonGiaLonNhat;
Sub query với câu lệnh INSERT
INSERT INTO table_name [ (column1 [, column2 ]) ]
SELECT [ *|column1 [, column2 ]
FROM table1 [, table2 ]
[ WHERE VALUE OPERATOR ]
Ví dụ:
INSERT INTO KhachHang_Backup
SELECT * FROM KhachHang
WHERE MaKH IN (
SELECT MaKH FROM HoaDon
);
Sub query với câu lệnh UPDATE
UPDATE table
SET column_name = new_value
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]
Ví dụ:
UPDATE NhanVien
SET Luong = Luong * 1.25
WHERE Tuoi IN (SELECT Tuoi FROM
KhachHang_Backup
WHERE Tuoi >= 27 );
Sub query với câu lệnh DELETE
DELETE FROM TABLE_NAME
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]
Ví dụ:
DELETE FROM KhachHang
WHERE Tuoi IN (SELECT Tuoi FROM
KhachHang_Backup
WHERE Tuoi >= 38 );
Tham khảo
• https://www.sqlservertutorial.net/sql-
server-basics/sql-server-subquery
• https://www.sqltutorial.org
Q&A

You might also like