SlideShare a Scribd company logo
1 of 27
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
CÁC CHUYÊN ĐỀ KHÁC
Hà Nội, 2/2020
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
NỘI DUNG HỌC
• SUBQUERY TRONG SQL
• TẠO VÀ SỬ DỤNG VIEW
• MỆNH ĐỀ WITH TRONG SQL
Confidential – Oracle Restricted 2
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
SUBQUERY
• Ngoài các phép nối và các toán tử tập hợp, SQL cung cấp một cách khác để
trả lại dữ liệu từ nhiều bảng gọi là truy vấn con (subquery).
• Khi một câu lệnh SELECT được sử dụng trong một câu lệnh khác, câu lệnh
SELECT bên trong được gọi là truy vấn con (subquery)
• Những truy vấn con có thể nằm trong mệnh đề WHERE, mệnh đề FROM,
hoặc mệnh đề SELECT.
Confidential – Oracle Restricted 3
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
SUBQUERY
Cú pháp:
SELECT <Danh sách các cột>
FROM <Danh sách các bảng>
WHERE OPERATOR
(SELECT <Danh sách các cột>
FROM <Danh sách các bảng>
[WHERE])
Để đưa ra kết quả cuối cùng, SQL thực hiện các bước sau:
• Thực thi truy vấn con trong mệnh đề FROM.
• Sử dụng kết quả của truy vấn con và thực hiện truy vấn bên ngoài.
Confidential – Oracle Restricted 4
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
SUBQUERY
Ta có thể sử dụng một truy vấn con (subquery) ở nhiều nơi như:
• Subquery thay cho một biểu thức
• Subquery với toán tử (=, IN, NOT IN, EXISTS, NOT EXISTS)
• Subquery trong mệnh đề FROM.
CÁC LOẠI SUBQUERY TRONG SQL
Confidential – Oracle Restricted 5
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
SUBQUERY
• Nếu truy vấn con trả về một giá trị duy nhất, nó có thể được sử dụng ở bất kỳ nơi nào
mà biểu thức được sử dụng
• Thường sử dụng với hàm tổng hợp: SUM, COUNT, MIN, hoặc MAX
SUBQUERY THAY CHO MỘT BIỂU THỨC
Confidential – Oracle Restricted 6
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
SUBQUERY
Ví dụ: Tìm đơn hàng có giá trị lớn nhất theo từng khách hàng
SELECT
customer_id,
name as customer_name,
(SELECT max(o.total) FROM orders o
WHERE cst.customer_id = o.customer_id
) AS max_total
FROM customers cst
ORDER BY customer_id desc;
SUBQUERY THAY CHO MỘT BIỂU THỨC
Confidential – Oracle Restricted 7
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
SUBQUERY
• Truy vấn con được sử dụng với toán tử trả về một hoặc tập hợp các giá trị.
• Sau khi truy vấn con trả về các giá trị, truy vấn bên ngoài sẽ sử dụng chúng.
SUBQUERY VỚI TOÁN TỬ
Confidential – Oracle Restricted 8
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
SUBQUERY
Ví dụ: Lấy ra sản phẩm có giá bán niêm yết (list_price ) là lớn nhất
SELECT product_id,
product_name,
list_price
FROM products
WHERE list_price = (SELECT MAX(list_price) FROM products );
SUBQUERY VỚI TOÁN TỬ =
Confidential – Oracle Restricted 9
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
SUBQUERY
Ví dụ: Lấy ra tất cả sản phẩm có danh mục là ‘CPU’ và 'Mother Board‘
SELECT product_id, product_name
FROM products
WHERE category_id IN (
SELECT category_id
FROM product_categories
WHERE category_name = 'CPU'
OR category_name = 'Mother Board'
);
SUBQUERY VỚI TOÁN TỬ IN (NOT IN)
Confidential – Oracle Restricted 10
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
SUBQUERY
Ví dụ: Lấy ra những khách hàng đã mua sản phẩm trong năm 2017:
SELECT customer_id, name
FROM customers c
WHERE EXISTS (
SELECT customer_id
FROM orders o
WHERE o.customer_id = c.customer_id
AND EXTRACT (YEAR FROM o.order_date) = 2017
)
SUBQUERY VỚI TOÁN TỬ EXISTS (NOT EXISTS)
Confidential – Oracle Restricted 11
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
SUBQUERY
• Khi các truy vấn con được sử dụng trong mệnh đề FROM, chúng hoạt động như một
bảng tạm mà ta có thể sử dụng để nối với các bảng khác
• Truy vấn mà bạn đặt trong mệnh đề FROM phải có bí danh bảng.
SUBQUERY TRONG MỆNH ĐỀ FROM
Confidential – Oracle Restricted 12
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
SUBQUERY
Ví dụ: Lấy ra tổng giá trị đơn hàng đã bán theo nhân viên
SELECT employees.employee_id, total_sale.total
FROM employees
JOIN (
SELECT sum(total) as total, salesman_id
FROM orders
GROUP BY salesman_id ) total_sale
ON total_sale.salesman_id = employees.employee_id
SUBQUERY TRONG MỆNH ĐỀ FROM
Confidential – Oracle Restricted 13
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
SUBQUERY
Ví dụ: Cách viết khác
SELECT employees.employee_id, total_sale.total
FROM
(SELECT sum(total) as total, salesman_id
FROM orders
GROUP BY salesman_id ) total_sale
JOIN employees
ON total_sale.salesman_id = employees.employee_id
SUBQUERY TRONG MỆNH ĐỀ FROM
Confidential – Oracle Restricted 14
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
WITH
• Mệnh đề With cho phép gán tên cho khối truy vấn con (Subquery).
• Có thể tái sử dụng lại nhiều lần, tham chiếu khối truy vấn con đó ở nhiều
vị trí trong câu truy vấn bằng cách chỉ định tên truy vấn
• Mệnh đề With đơn giản hóa những câu truy vấn phức tạp
• Mệnh đề With có thể cải thiện được hiệu suất truy vấn
WITH LÀ GÌ?
Confidential – Oracle Restricted 15
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
WITH
Mệnh đề WITH cho phép:
• Có thể gọi không giới hạn số lần.
• Có thể tạo ra bất kỳ số lượng khối truy vấn con.
• Một khối truy vấn con có thể tham chiếu đến một khối truy vấn con khác.
WITH LÀ GÌ?
Confidential – Oracle Restricted 16
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
WITH
Cú pháp:
WITH <Tên truy vấn con_1> AS
(
SELECT <Tên cột>
FROM <Tên bảng>
[WHERE dieu_kien_join]
)
SELECT <Tên cột> FROM <Tên truy vấn con_1>
Chú ý: Khi khai báo nhiều khối truy vấn con thì các khối ngăn cách nhau bằng dấu ‘,’ và tên không
được trùng nhau
WITH CTE1 AS (SELECT A, B FROM TABLE1),
CTE2 AS (SELECT C, C FROM TABLE2)
SỬ DỤNG WITH
Confidential – Oracle Restricted 17
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
WITH
Ví dụ: Đối với mỗi nhân viên, chúng ta muốn biết có bao nhiêu người làm trong bộ phận
của họ
WITH DEPT_COUNT AS
(
SELECT job_title, COUNT(*) Dept_Count
FROM employees
GROUP BY job_title
)
SELECT E1.first_name || ' ' || E1.last_name, E2.Dept_Count
FROM employees E1, DEPT_COUNT E2
WHERE E1.job_title = E2.job_title;
SỬ DỤNG WITH
Confidential – Oracle Restricted 18
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
VIEW
• View là một dạng table đặc biệt, là một table ảo và không hề tồn tại trong danh sách
table vật lý
• Nó được tạo ra khi câu lệnh được thực hiện
• Đặc điểm của View là dữ liệu của nó có thể lấy từ nhiều bảng khác nhau, bởi vì nó
được tạo ra từ câu lệnh select, mà trong lệnh select thì có thẻ join nhiều bảng
VIEW LÀ GÌ?
Confidential – Oracle Restricted 19
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
VIEW
• View thường được sử dụng để giới hạn quyền truy cập vào dữ liệu.
• Sử dụng view để tổng hợp dữ liệu từ các bảng khác nhau và có thể được sử dụng để
tạo các báo cáo.
VIEW SỬ DỤNG KHI NÀO?
Confidential – Oracle Restricted 20
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
VIEW
Cú pháp:
CREATE VIEW <Tên View> AS
SELECT <Tên cột>,
FROM <Tên bảng>
WHERE <Điều kiện>;
Chú ý: Có thể thêm nhiều bảng trong câu lệnh SELECT tương tự như cách sử dụng chúng
trong một truy vấn SQL SELECT bình thường
TẠO VIEW
Confidential – Oracle Restricted 21
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
VIEW
Ví dụ: Tạo một view với dữ liệu là tất cả các đơn hàng của khách hàng có hạn mức tín
dụng >= 400
CREATE VIEW cust_orders AS
SELECT customers.customer_id, customers.name
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id
WHERE customers.credit_limit >= 400;
TẠO VIEW
Confidential – Oracle Restricted 22
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
VIEW
• Truy vấn dữ liệu trong view tưởng tự như cách truy vấn bảng
Cú pháp:
SELECT <Tên cột>
FROM <Tên View>
Ví dụ: Lấy tất cả dữ liệu từ view cust_orders
SELECT * FROM cust_orders
TRUY VẤN DỮ LIỆU TRONG VIEW
Confidential – Oracle Restricted 23
Để cập nhật View thì bạn sẽ dùng lệnh CREATE OR REPLACE VIEW, lệnh này nó sẽ tạo view mới nếu chưa tồn tại, hoặc cập nhật view cũ nếu đã tồn tại.
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
VIEW
• Dùng lệnh CREATE OR REPLACE VIEW để cập nhật dữ liệu trong View, lệnh này nó sẽ
tạo view mới nếu chưa tồn tại, hoặc cập nhật view cũ nếu đã tồn tại.
Cú pháp:
CREATE OR REPLACE VIEW <Tên View> AS
SELECT <Tên cột>,
FROM <Tên bảng>
WHERE <Điều kiện>;
CẬP NHẬT DỮ LIỆU TRONG VIEW
Confidential – Oracle Restricted 24
Để cập nhật View thì bạn sẽ dùng lệnh CREATE OR REPLACE VIEW, lệnh này nó sẽ tạo view mới nếu chưa tồn tại, hoặc cập nhật view cũ nếu đã tồn tại.
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
VIEW
Ví dụ: Cập nhật view với dữ liệu là tất cả các đơn hàng của khách hàng có hạn mức tín
dụng < 400
CREATE OR REPLACE VIEW cust_orders AS
SELECT customers.customer_id, customers.name
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id
WHERE customers.credit_limit < 400;
CẬP NHẬT DỮ LIỆU TRONG VIEW
Confidential – Oracle Restricted 25
Để cập nhật View thì bạn sẽ dùng lệnh CREATE OR REPLACE VIEW, lệnh này nó sẽ tạo view mới nếu chưa tồn tại, hoặc cập nhật view cũ nếu đã tồn tại.
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
VIEW
• Lệnh DROP VIEW được dùng để xóa một View và tất cả các dữ liệu trong View
Cú pháp:
DROP VIEW <Tên View>;
Ví dụ: Xóa view cust_orders
DROP VIEW cust_orders;
XÓA VIEW
Confidential – Oracle Restricted 26
Để cập nhật View thì bạn sẽ dùng lệnh CREATE OR REPLACE VIEW, lệnh này nó sẽ tạo view mới nếu chưa tồn tại, hoặc cập nhật view cũ nếu đã tồn tại.
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. |
Thank You!

More Related Content

Featured

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 

Featured (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

BÀI 7 - CHUYÊN ĐỀ KHÁC.pptx

  • 1. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | CÁC CHUYÊN ĐỀ KHÁC Hà Nội, 2/2020
  • 2. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | NỘI DUNG HỌC • SUBQUERY TRONG SQL • TẠO VÀ SỬ DỤNG VIEW • MỆNH ĐỀ WITH TRONG SQL Confidential – Oracle Restricted 2
  • 3. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | SUBQUERY • Ngoài các phép nối và các toán tử tập hợp, SQL cung cấp một cách khác để trả lại dữ liệu từ nhiều bảng gọi là truy vấn con (subquery). • Khi một câu lệnh SELECT được sử dụng trong một câu lệnh khác, câu lệnh SELECT bên trong được gọi là truy vấn con (subquery) • Những truy vấn con có thể nằm trong mệnh đề WHERE, mệnh đề FROM, hoặc mệnh đề SELECT. Confidential – Oracle Restricted 3
  • 4. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | SUBQUERY Cú pháp: SELECT <Danh sách các cột> FROM <Danh sách các bảng> WHERE OPERATOR (SELECT <Danh sách các cột> FROM <Danh sách các bảng> [WHERE]) Để đưa ra kết quả cuối cùng, SQL thực hiện các bước sau: • Thực thi truy vấn con trong mệnh đề FROM. • Sử dụng kết quả của truy vấn con và thực hiện truy vấn bên ngoài. Confidential – Oracle Restricted 4
  • 5. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | SUBQUERY Ta có thể sử dụng một truy vấn con (subquery) ở nhiều nơi như: • Subquery thay cho một biểu thức • Subquery với toán tử (=, IN, NOT IN, EXISTS, NOT EXISTS) • Subquery trong mệnh đề FROM. CÁC LOẠI SUBQUERY TRONG SQL Confidential – Oracle Restricted 5
  • 6. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | SUBQUERY • Nếu truy vấn con trả về một giá trị duy nhất, nó có thể được sử dụng ở bất kỳ nơi nào mà biểu thức được sử dụng • Thường sử dụng với hàm tổng hợp: SUM, COUNT, MIN, hoặc MAX SUBQUERY THAY CHO MỘT BIỂU THỨC Confidential – Oracle Restricted 6
  • 7. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | SUBQUERY Ví dụ: Tìm đơn hàng có giá trị lớn nhất theo từng khách hàng SELECT customer_id, name as customer_name, (SELECT max(o.total) FROM orders o WHERE cst.customer_id = o.customer_id ) AS max_total FROM customers cst ORDER BY customer_id desc; SUBQUERY THAY CHO MỘT BIỂU THỨC Confidential – Oracle Restricted 7
  • 8. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | SUBQUERY • Truy vấn con được sử dụng với toán tử trả về một hoặc tập hợp các giá trị. • Sau khi truy vấn con trả về các giá trị, truy vấn bên ngoài sẽ sử dụng chúng. SUBQUERY VỚI TOÁN TỬ Confidential – Oracle Restricted 8
  • 9. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | SUBQUERY Ví dụ: Lấy ra sản phẩm có giá bán niêm yết (list_price ) là lớn nhất SELECT product_id, product_name, list_price FROM products WHERE list_price = (SELECT MAX(list_price) FROM products ); SUBQUERY VỚI TOÁN TỬ = Confidential – Oracle Restricted 9
  • 10. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | SUBQUERY Ví dụ: Lấy ra tất cả sản phẩm có danh mục là ‘CPU’ và 'Mother Board‘ SELECT product_id, product_name FROM products WHERE category_id IN ( SELECT category_id FROM product_categories WHERE category_name = 'CPU' OR category_name = 'Mother Board' ); SUBQUERY VỚI TOÁN TỬ IN (NOT IN) Confidential – Oracle Restricted 10
  • 11. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | SUBQUERY Ví dụ: Lấy ra những khách hàng đã mua sản phẩm trong năm 2017: SELECT customer_id, name FROM customers c WHERE EXISTS ( SELECT customer_id FROM orders o WHERE o.customer_id = c.customer_id AND EXTRACT (YEAR FROM o.order_date) = 2017 ) SUBQUERY VỚI TOÁN TỬ EXISTS (NOT EXISTS) Confidential – Oracle Restricted 11
  • 12. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | SUBQUERY • Khi các truy vấn con được sử dụng trong mệnh đề FROM, chúng hoạt động như một bảng tạm mà ta có thể sử dụng để nối với các bảng khác • Truy vấn mà bạn đặt trong mệnh đề FROM phải có bí danh bảng. SUBQUERY TRONG MỆNH ĐỀ FROM Confidential – Oracle Restricted 12
  • 13. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | SUBQUERY Ví dụ: Lấy ra tổng giá trị đơn hàng đã bán theo nhân viên SELECT employees.employee_id, total_sale.total FROM employees JOIN ( SELECT sum(total) as total, salesman_id FROM orders GROUP BY salesman_id ) total_sale ON total_sale.salesman_id = employees.employee_id SUBQUERY TRONG MỆNH ĐỀ FROM Confidential – Oracle Restricted 13
  • 14. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | SUBQUERY Ví dụ: Cách viết khác SELECT employees.employee_id, total_sale.total FROM (SELECT sum(total) as total, salesman_id FROM orders GROUP BY salesman_id ) total_sale JOIN employees ON total_sale.salesman_id = employees.employee_id SUBQUERY TRONG MỆNH ĐỀ FROM Confidential – Oracle Restricted 14
  • 15. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | WITH • Mệnh đề With cho phép gán tên cho khối truy vấn con (Subquery). • Có thể tái sử dụng lại nhiều lần, tham chiếu khối truy vấn con đó ở nhiều vị trí trong câu truy vấn bằng cách chỉ định tên truy vấn • Mệnh đề With đơn giản hóa những câu truy vấn phức tạp • Mệnh đề With có thể cải thiện được hiệu suất truy vấn WITH LÀ GÌ? Confidential – Oracle Restricted 15
  • 16. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | WITH Mệnh đề WITH cho phép: • Có thể gọi không giới hạn số lần. • Có thể tạo ra bất kỳ số lượng khối truy vấn con. • Một khối truy vấn con có thể tham chiếu đến một khối truy vấn con khác. WITH LÀ GÌ? Confidential – Oracle Restricted 16
  • 17. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | WITH Cú pháp: WITH <Tên truy vấn con_1> AS ( SELECT <Tên cột> FROM <Tên bảng> [WHERE dieu_kien_join] ) SELECT <Tên cột> FROM <Tên truy vấn con_1> Chú ý: Khi khai báo nhiều khối truy vấn con thì các khối ngăn cách nhau bằng dấu ‘,’ và tên không được trùng nhau WITH CTE1 AS (SELECT A, B FROM TABLE1), CTE2 AS (SELECT C, C FROM TABLE2) SỬ DỤNG WITH Confidential – Oracle Restricted 17
  • 18. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | WITH Ví dụ: Đối với mỗi nhân viên, chúng ta muốn biết có bao nhiêu người làm trong bộ phận của họ WITH DEPT_COUNT AS ( SELECT job_title, COUNT(*) Dept_Count FROM employees GROUP BY job_title ) SELECT E1.first_name || ' ' || E1.last_name, E2.Dept_Count FROM employees E1, DEPT_COUNT E2 WHERE E1.job_title = E2.job_title; SỬ DỤNG WITH Confidential – Oracle Restricted 18
  • 19. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | VIEW • View là một dạng table đặc biệt, là một table ảo và không hề tồn tại trong danh sách table vật lý • Nó được tạo ra khi câu lệnh được thực hiện • Đặc điểm của View là dữ liệu của nó có thể lấy từ nhiều bảng khác nhau, bởi vì nó được tạo ra từ câu lệnh select, mà trong lệnh select thì có thẻ join nhiều bảng VIEW LÀ GÌ? Confidential – Oracle Restricted 19
  • 20. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | VIEW • View thường được sử dụng để giới hạn quyền truy cập vào dữ liệu. • Sử dụng view để tổng hợp dữ liệu từ các bảng khác nhau và có thể được sử dụng để tạo các báo cáo. VIEW SỬ DỤNG KHI NÀO? Confidential – Oracle Restricted 20
  • 21. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | VIEW Cú pháp: CREATE VIEW <Tên View> AS SELECT <Tên cột>, FROM <Tên bảng> WHERE <Điều kiện>; Chú ý: Có thể thêm nhiều bảng trong câu lệnh SELECT tương tự như cách sử dụng chúng trong một truy vấn SQL SELECT bình thường TẠO VIEW Confidential – Oracle Restricted 21
  • 22. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | VIEW Ví dụ: Tạo một view với dữ liệu là tất cả các đơn hàng của khách hàng có hạn mức tín dụng >= 400 CREATE VIEW cust_orders AS SELECT customers.customer_id, customers.name FROM customers INNER JOIN orders ON customers.customer_id = orders.customer_id WHERE customers.credit_limit >= 400; TẠO VIEW Confidential – Oracle Restricted 22
  • 23. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | VIEW • Truy vấn dữ liệu trong view tưởng tự như cách truy vấn bảng Cú pháp: SELECT <Tên cột> FROM <Tên View> Ví dụ: Lấy tất cả dữ liệu từ view cust_orders SELECT * FROM cust_orders TRUY VẤN DỮ LIỆU TRONG VIEW Confidential – Oracle Restricted 23 Để cập nhật View thì bạn sẽ dùng lệnh CREATE OR REPLACE VIEW, lệnh này nó sẽ tạo view mới nếu chưa tồn tại, hoặc cập nhật view cũ nếu đã tồn tại.
  • 24. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | VIEW • Dùng lệnh CREATE OR REPLACE VIEW để cập nhật dữ liệu trong View, lệnh này nó sẽ tạo view mới nếu chưa tồn tại, hoặc cập nhật view cũ nếu đã tồn tại. Cú pháp: CREATE OR REPLACE VIEW <Tên View> AS SELECT <Tên cột>, FROM <Tên bảng> WHERE <Điều kiện>; CẬP NHẬT DỮ LIỆU TRONG VIEW Confidential – Oracle Restricted 24 Để cập nhật View thì bạn sẽ dùng lệnh CREATE OR REPLACE VIEW, lệnh này nó sẽ tạo view mới nếu chưa tồn tại, hoặc cập nhật view cũ nếu đã tồn tại.
  • 25. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | VIEW Ví dụ: Cập nhật view với dữ liệu là tất cả các đơn hàng của khách hàng có hạn mức tín dụng < 400 CREATE OR REPLACE VIEW cust_orders AS SELECT customers.customer_id, customers.name FROM customers INNER JOIN orders ON customers.customer_id = orders.customer_id WHERE customers.credit_limit < 400; CẬP NHẬT DỮ LIỆU TRONG VIEW Confidential – Oracle Restricted 25 Để cập nhật View thì bạn sẽ dùng lệnh CREATE OR REPLACE VIEW, lệnh này nó sẽ tạo view mới nếu chưa tồn tại, hoặc cập nhật view cũ nếu đã tồn tại.
  • 26. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | VIEW • Lệnh DROP VIEW được dùng để xóa một View và tất cả các dữ liệu trong View Cú pháp: DROP VIEW <Tên View>; Ví dụ: Xóa view cust_orders DROP VIEW cust_orders; XÓA VIEW Confidential – Oracle Restricted 26 Để cập nhật View thì bạn sẽ dùng lệnh CREATE OR REPLACE VIEW, lệnh này nó sẽ tạo view mới nếu chưa tồn tại, hoặc cập nhật view cũ nếu đã tồn tại.
  • 27. Copyright © 2019, Oracle and/or its affiliates. All rights reserved. | Thank You!