Sử dụng Select trong SQL, Select distinct trong SQL
Câu lệnh SELECT là một trong những câu lệnh quan trọng nhất trong ngôn ngữ truy vấn cơ sở dữ liệu SQL. Nó được sử dụng để truy vấn dữ liệu từ một hoặc nhiều bảng.

Cấu trúc cơ bản:
SELECT [column_list] FROM table_name;
Giải thích:
- SELECT: Từ khóa cho biết bạn muốn truy vấn dữ liệu.
- column_list: Danh sách các cột mà bạn muốn lấy dữ liệu. Bạn có thể sử dụng dấu * để lấy tất cả các cột.
- FROM: Từ khóa cho biết bạn muốn truy vấn dữ liệu từ bảng nào.
- table_name: Tên của bảng mà bạn muốn truy vấn dữ liệu.
Ví dụ:
Lấy tất cả dữ liệu từ bảng customers:
SELECT * FROM customers;
Lấy các cột name, email và phone từ bảng customers:
SELECT name, email, phone FROM customers;
Lọc dữ liệu:
Bạn có thể sử dụng mệnh đề WHERE để lọc dữ liệu dựa trên các điều kiện.
Cấu trúc:
SELECT [column_list] FROM table_name WHERE condition;
Ví dụ:
Lấy tất cả khách hàng từ Hà Nội:
SELECT * FROM customers WHERE city = 'Hanoi';
Lấy 10 khách hàng mới nhất:
SELECT * FROM customers ORDER BY created_at DESC LIMIT 10;
Sắp xếp dữ liệu:
Bạn có thể sử dụng mệnh đề ORDER BY để sắp xếp dữ liệu theo một hoặc nhiều cột.
Cấu trúc:
SELECT [column_list] FROM table_name ORDER BY column_name [ASC | DESC];
Ví dụ:
Sắp xếp khách hàng theo tên theo thứ tự bảng chữ cái:
SELECT * FROM customers ORDER BY name ASC;
Sắp xếp khách hàng theo doanh thu theo thứ tự giảm dần:
SELECT * FROM customers ORDER BY revenue DESC;
Hàm tổng hợp:
Bạn có thể sử dụng các hàm tổng hợp như SUM, AVG, COUNT để tính toán dữ liệu.
Cấu trúc:
SELECT aggregate_function(column_name) FROM table_name;
Ví dụ:
Tính tổng doanh thu của tất cả khách hàng: SELECT SUM(revenue) FROM customers; Tính số lượng khách hàng: SELECT COUNT(*) FROM customers;
Kết hợp bảng:
Bạn có thể sử dụng mệnh đề JOIN để kết hợp dữ liệu từ hai hoặc nhiều bảng.
Cấu trúc:
SELECT column_list FROM table_name1 JOIN table_name2 ON join_condition;
Ví dụ:
Lấy tên và email của tất cả khách hàng cùng với tên quốc gia của họ:
SELECT customers.name, customers.email, countries.name FROM customers JOIN countries ON customers.country_id = countries.id;
Select có sử dụng DISTINCT: mệnh đề sẽ trả về dữ liệu có giá trị duy nhất của cột
Cấu trúc như sau:
SELECT DISTINCT column1, column2, ... FROM table_name;
Ví dụ:
Trả về giá trị duy nhất theo Country của bảng Customers.
SELECT DISTINCT Country FROM Customers;
Chúc các bạn thành công !
- Tạo Non-Cdb Oracle Database trong Silent Mode Oracle database 19c
- Q30. Which three actions are performed by Oracle Preinstallation RPM, oracle-database-server-xxxx-preinstall, for Oracle Grid Infrastructure, where xxxx is the Oracle version and release?
- Quản trị Flash Recovery Area trong Oracle 19c Rac Database
- Lợi Ích và Thách Thức Của Việc Chấp Nhận Bitcoin Trong Thanh Toán
- Q104 Which statement is true about the INTERSECT operator used in compound queries?

Stockholm - Elementor Theme for Creative Business & WooCommerce
Shopkeeper - Multi-Purpose WooCommerce Theme
JupiterX - Multipurpose WordPress & WooCommerce Theme
Bookory - Book Store WooCommerce Theme
Baby Toys And Accessories Store Shopify Theme
Rank Math SEO Pro
Sober - WooCommerce WordPress Theme
Duplicate Page - nhân bản page post nhanh chóng
Babyza - Kids Fashion Responsive Shopify Theme
The7 - Ultimate Website & Online Store Builder for WordPress and WooCommerce
Essentials - Multipurpose WordPress Theme
YITH WooCommerce Role Based Prices Premium
Careera - Recruitment agency Elementor Template Kit
Dropper - Sropshipping & Logistic Elementor Template Kit
Nandur - Environment Elementor Template Kit
Ronneby - High-Performance WordPress Theme
Porto | Multipurpose & WooCommerce Theme
Total - Responsive Multi-Purpose WordPress Theme
YITH Event Tickets for WooCommerce Premium
LayerSlider Premium
Avada Theme
Ave - Responsive Multi-Purpose WordPress Theme
WooCommerce Custom Fields
Sensei Content Drip
Boxstore - Multipurpose Magento Theme
WooCommerce Notification | Boost Your Sales
All in One SEO Pack Pro
YITH WooCommerce Email Templates Premium
Amely - Clean & Modern Magento 2 Theme
Angara - Responsive Magento X & X Theme
WooCommerce Aramex
MinimogWP - The High Converting eCommerce WordPress Theme
The Retailer - Premium WooCommerce Theme
WooCommerce Australia Post Shipping Method
Haxico - Technology Responsive Magento Theme
Go Pricing
WooCommerce Group Coupons
Martfury - WooCommerce Marketplace WordPress Theme
Mina - Responsive Furniture, Handmade Shop Magento 2 Theme