{"id":80860,"date":"2024-11-01T16:20:39","date_gmt":"2024-11-01T09:20:39","guid":{"rendered":"https:\/\/itviec.com\/blog\/?p=80860"},"modified":"2024-11-01T16:20:39","modified_gmt":"2024-11-01T09:20:39","slug":"select-trong-sql","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/select-trong-sql\/","title":{"rendered":"SELECT trong SQL: H\u01b0\u1edbng d\u1eabn c\u00e1c c\u00e1ch \u1ee9ng d\u1ee5ng l\u1ec7nh SELECT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">N\u1ed9i dung b\u00e0i vi\u1ebft<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/itviec.com\/blog\/select-trong-sql\/#SELECT_trong_SQL_la_gi\" >SELECT trong SQL l\u00e0 g\u00ec?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/itviec.com\/blog\/select-trong-sql\/#Cu_phap_SELECT_trong_SQL_co_ban_truy_xuat_du_lieu_tu_mot_bang\" >C\u00fa ph\u00e1p SELECT trong SQL c\u01a1 b\u1ea3n (truy xu\u1ea5t d\u1eef li\u1ec7u t\u1eeb m\u1ed9t b\u1ea3ng)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/itviec.com\/blog\/select-trong-sql\/#SELECT_trong_SQL_voi_dieu_kien_WHERE\" >SELECT trong SQL v\u1edbi \u0111i\u1ec1u ki\u1ec7n WHERE<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/itviec.com\/blog\/select-trong-sql\/#SELECT_trong_SQL_voi_cac_ham_tong_hop\" >SELECT trong SQL v\u1edbi c\u00e1c h\u00e0m t\u1ed5ng h\u1ee3p<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/itviec.com\/blog\/select-trong-sql\/#SELECT_trong_SQL_voi_ORDER_BY\" >SELECT trong SQL v\u1edbi ORDER BY<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/itviec.com\/blog\/select-trong-sql\/#SELECT_trong_SQL_voi_GROUP_BY\" >SELECT trong SQL v\u1edbi GROUP BY<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/itviec.com\/blog\/select-trong-sql\/#SELECT_trong_SQL_voi_menh_de_HAVING\" >SELECT trong SQL v\u1edbi m\u1ec7nh \u0111\u1ec1 HAVING<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/itviec.com\/blog\/select-trong-sql\/#Gioi_han_so_luong_ket_qua_voi_LIMIT\" >Gi\u1edbi h\u1ea1n s\u1ed1 l\u01b0\u1ee3ng k\u1ebft qu\u1ea3 v\u1edbi LIMIT<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/itviec.com\/blog\/select-trong-sql\/#Ket_hop_du_lieu_tu_nhieu_bang_voi_SELECT_va_JOIN\" >K\u1ebft h\u1ee3p d\u1eef li\u1ec7u t\u1eeb nhi\u1ec1u b\u1ea3ng v\u1edbi SELECT v\u00e0 JOIN<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/itviec.com\/blog\/select-trong-sql\/#Cau_hoi_thuong_gap_ve_SELECT_trong_SQL\" >C\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 SELECT trong SQL<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/itviec.com\/blog\/select-trong-sql\/#Tong_ket_SELECT_trong_SQL\" >T\u1ed5ng k\u1ebft SELECT trong SQL<\/a><\/li><\/ul><\/nav><\/div>\n<p><em><strong>L\u1ec7nh SELECT trong SQL l\u00e0 m\u1ed9t c\u00e2u l\u1ec7nh quan tr\u1ecdng v\u00e0 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng ph\u1ed5 bi\u1ebfn nh\u1ea5t \u0111\u1ec3 truy xu\u1ea5t d\u1eef li\u1ec7u t\u1eeb c\u01a1 s\u1edf d\u1eef li\u1ec7u. T\u1eeb nh\u1eefng truy v\u1ea5n \u0111\u01a1n gi\u1ea3n \u0111\u1ec3 l\u1ea5y d\u1eef li\u1ec7u cho \u0111\u1ebfn nh\u1eefng truy v\u1ea5n ph\u1ee9c t\u1ea1p k\u1ebft h\u1ee3p d\u1eef li\u1ec7u t\u1eeb nhi\u1ec1u b\u1ea3ng, SELECT cho ph\u00e9p ng\u01b0\u1eddi d\u00f9ng linh ho\u1ea1t trong vi\u1ec7c l\u1ecdc, s\u1eafp x\u1ebfp v\u00e0 t\u00ednh to\u00e1n d\u1eef li\u1ec7u.<\/strong><\/em><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ecdc b\u00e0i vi\u1ebft n\u00e0y \u0111\u1ec3 hi\u1ec3u r\u00f5 h\u01a1n v\u1ec1:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">SELECT trong SQL<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">M\u1ed9t s\u1ed1 c\u00fa ph\u00e1p c\u1ee7a SELECT<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"SELECT_trong_SQL_la_gi\"><\/span><b>SELECT trong SQL l\u00e0 g\u00ec?<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">L\u1ec7nh SELECT trong SQL l\u00e0 m\u1ed9t trong nh\u1eefng l\u1ec7nh c\u01a1 b\u1ea3n v\u00e0 quan tr\u1ecdng nh\u1ea5t \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 truy xu\u1ea5t d\u1eef li\u1ec7u t\u1eeb m\u1ed9t ho\u1eb7c nhi\u1ec1u b\u1ea3ng trong c\u01a1 s\u1edf d\u1eef li\u1ec7u. Ch\u00fang ta c\u00f3 th\u1ec3 l\u1ea5y to\u00e0n b\u1ed9 d\u1eef li\u1ec7u t\u1eeb b\u1ea3ng ho\u1eb7c ch\u1ec9 nh\u1eefng d\u1eef li\u1ec7u th\u1ecfa m\u00e3n c\u00e1c \u0111i\u1ec1u ki\u1ec7n c\u1ee5 th\u1ec3. K\u1ebft qu\u1ea3 c\u1ee7a l\u1ec7nh SELECT l\u00e0 m\u1ed9t t\u1eadp h\u1ee3p d\u1eef li\u1ec7u (result-set) \u0111\u01b0\u1ee3c tr\u1ea3 v\u1ec1 sau khi th\u1ef1c hi\u1ec7n truy v\u1ea5n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">M\u1eb7c d\u00f9 m\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\"> th\u01b0\u1eddng \u0111\u01b0\u1ee3c vi\u1ebft \u0111\u1ea7u ti\u00ean trong c\u00e2u l\u1ec7nh, nh\u01b0ng n\u00f3 kh\u00f4ng ph\u1ea3i l\u00e0 ph\u1ea7n \u0111\u1ea7u ti\u00ean m\u00e0 h\u1ec7 th\u1ed1ng c\u01a1 s\u1edf d\u1eef li\u1ec7u th\u1ef1c s\u1ef1 x\u1eed l\u00fd. Khi b\u1ea1n vi\u1ebft truy v\u1ea5n, n\u00f3 s\u1ebd \u0111\u01b0\u1ee3c x\u1eed l\u00fd theo c\u00e1c b\u01b0\u1edbc sau:\u00a0<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>FROM \/ JOIN:<\/b><span style=\"font-weight: 400;\"> Truy xu\u1ea5t v\u00e0 k\u1ebft n\u1ed1i d\u1eef li\u1ec7u t\u1eeb b\u1ea3ng.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>WHERE:<\/b><span style=\"font-weight: 400;\"> L\u1ecdc c\u00e1c b\u1ea3n ghi (record) theo \u0111i\u1ec1u ki\u1ec7n c\u1ee5 th\u1ec3.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>GROUP BY:<\/b><span style=\"font-weight: 400;\"> Nh\u00f3m c\u00e1c b\u1ea3n ghi theo m\u1ed9t ho\u1eb7c nhi\u1ec1u c\u1ed9t.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>HAVING:<\/b><span style=\"font-weight: 400;\"> L\u1ecdc c\u00e1c nh\u00f3m d\u1eef li\u1ec7u sau khi nh\u00f3m.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>SELECT:<\/b><span style=\"font-weight: 400;\"> Ch\u1ecdn c\u00e1c c\u1ed9t c\u1ea7n \u0111\u1ec3 hi\u1ec3n th\u1ecb trong k\u1ebft qu\u1ea3.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>ORDER BY:<\/b><span style=\"font-weight: 400;\"> S\u1eafp x\u1ebfp d\u1eef li\u1ec7u theo th\u1ee9 t\u1ef1 c\u1ee5 th\u1ec3.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>LIMIT:<\/b><span style=\"font-weight: 400;\"> Gi\u1edbi h\u1ea1n s\u1ed1 l\u01b0\u1ee3ng b\u1ea3n ghi tr\u1ea3 v\u1ec1.<\/span><\/li>\n<\/ol>\n<p>Ti\u1ebfp theo \u0111\u00e2y, ta s\u1ebd c\u00f9ng t\u00ecm hi\u1ec3u chi ti\u1ebft v\u1ec1 c\u00e1c c\u00fa ph\u00e1p ph\u1ed5 bi\u1ebfn c\u1ee7a SELECT trong SQL qua c\u00e1c v\u00ed d\u1ee5 c\u1ee5 th\u1ec3 v\u1edbi h<span style=\"font-weight: 400;\">ai b\u1ea3ng d\u1eef li\u1ec7u sau:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">B\u1ea3ng customer:<\/span><\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><b>customer_id<\/b><\/td>\n<td><b>name<\/b><\/td>\n<td><b>city<\/b><\/td>\n<td><b>age<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">John Doe<\/span><\/td>\n<td><span style=\"font-weight: 400;\">New York<\/span><\/td>\n<td><span style=\"font-weight: 400;\">30<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">2<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Jane Roe<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Los Angeles<\/span><\/td>\n<td><span style=\"font-weight: 400;\">25<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Mike Lee<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Chicago<\/span><\/td>\n<td><span style=\"font-weight: 400;\">35<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">4<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Sarah Kim<\/span><\/td>\n<td><span style=\"font-weight: 400;\">New York<\/span><\/td>\n<td><span style=\"font-weight: 400;\">28<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">B\u1ea3ng order:<\/span><\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><b>order_id<\/b><\/td>\n<td><b>customer_id<\/b><\/td>\n<td><b>amount<\/b><\/td>\n<td><b>order_date<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">101<\/span><\/td>\n<td><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">250<\/span><\/td>\n<td><span style=\"font-weight: 400;\">2024-10-01<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">102<\/span><\/td>\n<td><span style=\"font-weight: 400;\">2<\/span><\/td>\n<td><span style=\"font-weight: 400;\">300<\/span><\/td>\n<td><span style=\"font-weight: 400;\">2024-10-02<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">103<\/span><\/td>\n<td><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td><span style=\"font-weight: 400;\">450<\/span><\/td>\n<td><span style=\"font-weight: 400;\">2024-10-03<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">104<\/span><\/td>\n<td><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">150<\/span><\/td>\n<td><span style=\"font-weight: 400;\">2024-10-04<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span class=\"ez-toc-section\" id=\"Cu_phap_SELECT_trong_SQL_co_ban_truy_xuat_du_lieu_tu_mot_bang\"><\/span><b>C\u00fa ph\u00e1p SELECT trong SQL c\u01a1 b\u1ea3n (truy xu\u1ea5t d\u1eef li\u1ec7u t\u1eeb m\u1ed9t b\u1ea3ng)<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">L\u00e0 c\u00fa ph\u00e1p \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 truy v\u1ea5n c\u00e1c c\u1ed9t t\u1eeb m\u1ed9t b\u1ea3ng c\u1ee5 th\u1ec3. C\u00f3 th\u1ec3 ch\u1ecdn c\u00e1c c\u1ed9t c\u1ee5 th\u1ec3 ho\u1eb7c t\u1ea5t c\u1ea3 c\u00e1c c\u1ed9t t\u1eeb b\u1ea3ng b\u1eb1ng c\u00e1ch li\u1ec7t k\u00ea t\u00ean c\u1ed9t trong m\u1ec7nh \u0111\u1ec1 SELECT.<\/span><\/p>\n<h3><b>C\u00fa ph\u00e1p 1<\/b><\/h3>\n<pre><span style=\"font-weight: 400;\">SELECT column1, column2, ...\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM table_name;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 truy xu\u1ea5t t\u1ea5t c\u1ea3 th\u00f4ng tin v\u1ec1 kh\u00e1ch h\u00e0ng, ta d\u00f9ng c\u00fa ph\u00e1p nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT customer_id, name, age\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM customers;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u1edbi c\u00fa ph\u00e1p tr\u00ean ta thu \u0111\u01b0\u1ee3c k\u1ebft qu\u1ea3 nh\u01b0 sau:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>customer_id<\/b><\/td>\n<td><b>name<\/b><\/td>\n<td><b>age<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">John Doe<\/span><\/td>\n<td><span style=\"font-weight: 400;\">30<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">2<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Jane Roe<\/span><\/td>\n<td><span style=\"font-weight: 400;\">25<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Mike Lee<\/span><\/td>\n<td><span style=\"font-weight: 400;\">35<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">4<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Sarah Kim<\/span><\/td>\n<td><span style=\"font-weight: 400;\">28<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>C\u00fa ph\u00e1p 2: Truy xu\u1ea5t t\u1ea5t c\u1ea3 c\u00e1c c\u1ed9t t\u1eeb m\u1ed9t b\u1ea3ng<\/b><\/h3>\n<pre><span style=\"font-weight: 400;\">SELECT *\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM table_name;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 truy xu\u1ea5t to\u00e0n b\u1ed9 d\u1eef li\u1ec7u t\u1eeb b\u1ea3ng <\/span><span style=\"font-weight: 400;\">customer<\/span><span style=\"font-weight: 400;\"> ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT * FROM customers;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">L\u1ec7nh tr\u00ean s\u1ebd tr\u1ea3 v\u1ec1 to\u00e0n b\u1ed9 d\u1eef li\u1ec7u t\u1eeb b\u1ea3ng <\/span><span style=\"font-weight: 400;\">customer<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<h3><b>C\u00fa ph\u00e1p 3: Alias<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">N\u1ebfu mu\u1ed1n g\u00e1n t\u00ean c\u1ed9t ho\u1eb7c trong k\u1ebft qu\u1ea3 tr\u1ea3 v\u1ec1, ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">Alias<\/span><span style=\"font-weight: 400;\"> v\u1edbi c\u00fa ph\u00e1p nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT column_name AS alias_name\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM table_name;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y t\u00ean v\u00e0 tu\u1ed5i c\u1ee7a kh\u00e1ch h\u00e0ng t\u1eeb b\u1ea3ng customers v\u00e0 \u0111\u1ed5i t\u00ean c\u00e1c c\u1ed9t n\u00e0y th\u00e0nh <\/span><i><span style=\"font-weight: 400;\">customer_name<\/span><\/i><span style=\"font-weight: 400;\"> v\u00e0 <\/span><i><span style=\"font-weight: 400;\">customer_age<\/span><\/i><span style=\"font-weight: 400;\">, ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng c\u00fa ph\u00e1p nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT name AS customer_name, age AS customer_age\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM customers;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u1edbi c\u00fa ph\u00e1p tr\u00ean ta thu \u0111\u01b0\u1ee3c k\u1ebft qu\u1ea3 nh\u01b0 sau:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>customer_name<\/b><\/td>\n<td><b>customer_age<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">John Doe<\/span><\/td>\n<td><span style=\"font-weight: 400;\">30<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Jane Doe<\/span><\/td>\n<td><span style=\"font-weight: 400;\">25<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Mike Lee<\/span><\/td>\n<td><span style=\"font-weight: 400;\">35<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Sarah Kim<\/span><\/td>\n<td><span style=\"font-weight: 400;\">28<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>C\u00fa ph\u00e1p 4: SELECT DISTINCT trong SQL<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">N\u1ebfu mu\u1ed1n k\u1ebft qu\u1ea3 tr\u1ea3 v\u1ec1 (c\u00e1c b\u1ea3n ghi) kh\u00f4ng b\u1ecb tr\u00f9ng l\u1eb7p, ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">DISTINCT<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5: \u0111\u1ec3 l\u1ea5y danh s\u00e1ch c\u00e1c th\u00e0nh ph\u1ed1 kh\u00e1ch h\u00e0ng c\u01b0 tr\u00fa v\u00e0 lo\u1ea1i b\u1ecf c\u00e1c gi\u00e1 tr\u1ecb tr\u00f9ng l\u1eb7p, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT DISTINCT city AS unique_city\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM customers;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u1edbi c\u00fa ph\u00e1p tr\u00ean ta thu \u0111\u01b0\u1ee3c k\u1ebft qu\u1ea3 nh\u01b0 sau:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>unique_city<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">New York<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Los Angeles<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Chicago<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span class=\"ez-toc-section\" id=\"SELECT_trong_SQL_voi_dieu_kien_WHERE\"><\/span><b>SELECT trong SQL v\u1edbi \u0111i\u1ec1u ki\u1ec7n WHERE<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">M\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\"> khi k\u1ebft h\u1ee3p v\u1edbi <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\"> s\u1ebd c\u00f3 ch\u1ee9c n\u0103ng l\u1ecdc d\u1eef li\u1ec7u d\u1ef1a tr\u00ean c\u00e1c \u0111i\u1ec1u ki\u1ec7n c\u1ee5 th\u1ec3.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT column1, column2, ...\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM table_name\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE condition;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\"><strong>V\u00ed d\u1ee5 1:<\/strong>\u00a0\u0110\u1ec3 truy xu\u1ea5t t\u1ea5t c\u1ea3 c\u00e1c kh\u00e1ch h\u00e0ng \u1edf th\u00e0nh ph\u1ed1 New York, ta d\u00f9ng c\u00fa ph\u00e1p nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT name, city\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM customers\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE city = 'New York';<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u1edbi c\u00fa ph\u00e1p tr\u00ean ta thu \u0111\u01b0\u1ee3c k\u1ebft qu\u1ea3 nh\u01b0 sau:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>name<\/b><\/td>\n<td><b>city<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Jane Roe<\/span><\/td>\n<td><span style=\"font-weight: 400;\">New York<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Sarah Kim<\/span><\/td>\n<td><span style=\"font-weight: 400;\">New York<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>V\u00ed d\u1ee5 2<\/b><span style=\"font-weight: 400;\">: \u0110\u1ec3 t\u00ecm ki\u1ebfm c\u00e1c gi\u00e1 tr\u1ecb trong m\u1ed9t c\u1ed9t c\u00f3 ch\u1ee9a m\u1ed9t chu\u1ed7i k\u00fd t\u1ef1 c\u1ee5 th\u1ec3, ta s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">LIKE<\/span><span style=\"font-weight: 400;\"> c\u00f9ng v\u1edbi m\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\">. V\u00ed d\u1ee5 \u0111\u1ec3 t\u00ecm kh\u00e1ch h\u00e0ng c\u00f3 t\u00ean b\u1eaft \u0111\u1ea7u b\u1eb1ng ch\u1eef &#8220;J&#8221;, ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT name\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM customers\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE name LIKE 'J%';<\/span><\/pre>\n<p><b>V\u00ed d\u1ee5 3<\/b><span style=\"font-weight: 400;\">: \u0110\u1ec3 l\u1ecdc k\u1ebft qu\u1ea3 d\u1ef1a tr\u00ean m\u1ed9t danh s\u00e1ch c\u00e1c gi\u00e1 tr\u1ecb c\u1ee5 th\u1ec3, ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">IN<\/span><span style=\"font-weight: 400;\"> c\u00f9ng v\u1edbi m\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\">. V\u00ed d\u1ee5, \u0111\u1ec3 t\u00ecm nh\u1eefng kh\u00e1ch h\u00e0ng \u1edf c\u00e1c th\u00e0nh ph\u1ed1 &#8220;New York&#8221; ho\u1eb7c &#8220;Chicago&#8221;, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT name, city\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM customers\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE city IN ('New York', 'Chicago');<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Ngo\u00e0i ra, WHERE c\u00f2n c\u00f3 th\u1ec3 k\u1ebft h\u1ee3p nhi\u1ec1u to\u00e1n t\u1eed kh\u00e1c gi\u00fap \u0111a d\u1ea1ng h\u00f3a \u0111i\u1ec1u ki\u1ec7n l\u1ecdc, nh\u01b0:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">To\u00e1n t\u1eed so s\u00e1nh: <\/span><span style=\"font-weight: 400;\">=,<\/span> <span style=\"font-weight: 400;\">!=,<\/span> <span style=\"font-weight: 400;\">&lt;,<\/span> <span style=\"font-weight: 400;\">&gt;,<\/span> <span style=\"font-weight: 400;\">&lt;=,<\/span> <span style=\"font-weight: 400;\">&gt;=<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">To\u00e1n t\u1eed logic: <\/span><span style=\"font-weight: 400;\">AND<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">OR<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">NOT<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">To\u00e1n t\u1eed gi\u1eefa c\u00e1c ph\u1ea1m vi: <\/span><span style=\"font-weight: 400;\">BETWEEN<\/span><\/li>\n<\/ul>\n<p><b>V\u00ed d\u1ee5 4<\/b><span style=\"font-weight: 400;\">: \u0110\u1ec3 l\u1ecdc k\u1ebft qu\u1ea3 theo m\u1ed9t gi\u00e1 tr\u1ecb l\u1edbn h\u01a1n, ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng to\u00e1n t\u1eed <\/span><span style=\"font-weight: 400;\">&gt;<\/span><span style=\"font-weight: 400;\"> v\u1edbi m\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\">. V\u00ed d\u1ee5, \u0111\u1ec3 t\u00ecm nh\u1eefng kh\u00e1ch h\u00e0ng c\u00f3 \u0111\u1ed9 tu\u1ed5i l\u1edbn h\u01a1n 30, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT name, age\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM customers\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE age &gt; 30;<\/span><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"SELECT_trong_SQL_voi_cac_ham_tong_hop\"><\/span><b>SELECT trong SQL v\u1edbi c\u00e1c h\u00e0m t\u1ed5ng h\u1ee3p<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">H\u00e0m t\u1ed5ng h\u1ee3p l\u00e0 c\u00e1c h\u00e0m cho ph\u00e9p t\u00ednh to\u00e1n d\u1eef li\u1ec7u tr\u00ean c\u00e1c nh\u00f3m b\u1ea3n ghi, m\u1ed9t s\u1ed1 h\u00e0m t\u1ed5ng h\u1ee3p l\u00e0 <\/span><span style=\"font-weight: 400;\">COUNT<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">SUM<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">AVG<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">MAX<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">MIN<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT aggregate_function(column)\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM table_name;<\/span><\/pre>\n<p><b>V\u00ed d\u1ee5 1<\/b><span style=\"font-weight: 400;\">: \u0110\u1ec3 t\u00ednh t\u1ed5ng s\u1ed1 ti\u1ec1n t\u1eeb c\u00e1c \u0111\u01a1n h\u00e0ng, ta d\u00f9ng c\u00fa ph\u00e1p nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT SUM(amount)\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM orders;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u1edbi c\u00fa ph\u00e1p tr\u00ean ta thu \u0111\u01b0\u1ee3c k\u1ebft qu\u1ea3 nh\u01b0 sau:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">SUM (AMOUNT)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">1150<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>V\u00ed d\u1ee5 2<\/b><span style=\"font-weight: 400;\">: \u0110\u1ec3 t\u00ednh t\u1ed5ng s\u1ed1 ti\u1ec1n c\u1ee7a c\u00e1c \u0111\u01a1n h\u00e0ng c\u00f3 gi\u00e1 tr\u1ecb tr\u00ean 200 ta s\u1ebd k\u1ebft h\u1ee3p v\u1edbi \u0111i\u1ec1u ki\u1ec7n WHERE v\u1edbi c\u00fa ph\u00e1p nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT SUM(amount)\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM orders\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE amount &gt; 200;<\/span><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"SELECT_trong_SQL_voi_ORDER_BY\"><\/span><b>SELECT trong SQL v\u1edbi ORDER BY<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">M\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">ORDER BY<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 s\u1eafp x\u1ebfp k\u1ebft qu\u1ea3 theo m\u1ed9t ho\u1eb7c nhi\u1ec1u c\u1ed9t, c\u00f3 th\u1ec3 s\u1eafp x\u1ebfp t\u0103ng d\u1ea7n (<\/span><span style=\"font-weight: 400;\">ASC<\/span><span style=\"font-weight: 400;\">) ho\u1eb7c gi\u1ea3m d\u1ea7n (<\/span><span style=\"font-weight: 400;\">DESC<\/span><span style=\"font-weight: 400;\">).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT column1, column2, ...\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM table_name\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">ORDER BY column [ASC|DESC];<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 truy xu\u1ea5t danh s\u00e1ch kh\u00e1ch h\u00e0ng v\u00e0 s\u1eafp x\u1ebfp theo tu\u1ed5i gi\u1ea3m d\u1ea7n, ta d\u00f9ng c\u00fa ph\u00e1p nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT name, age\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM customers\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">ORDER BY age DESC;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u1edbi c\u00fa ph\u00e1p tr\u00ean ta thu \u0111\u01b0\u1ee3c k\u1ebft qu\u1ea3 nh\u01b0 sau:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>name<\/b><\/td>\n<td><b>age<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Mike Lee<\/span><\/td>\n<td><span style=\"font-weight: 400;\">35<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">John Doe<\/span><\/td>\n<td><span style=\"font-weight: 400;\">30<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Sarah Kim<\/span><\/td>\n<td><span style=\"font-weight: 400;\">28<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Jane Roe<\/span><\/td>\n<td><span style=\"font-weight: 400;\">25<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span class=\"ez-toc-section\" id=\"SELECT_trong_SQL_voi_GROUP_BY\"><\/span><b>SELECT trong SQL v\u1edbi GROUP BY<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">M\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">GROUP BY<\/span><span style=\"font-weight: 400;\"> th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 nh\u00f3m c\u00e1c b\u1ea3n ghi d\u1ef1a tr\u00ean gi\u00e1 tr\u1ecb c\u1ee7a m\u1ed9t ho\u1eb7c nhi\u1ec1u c\u1ed9t, th\u01b0\u1eddng k\u1ebft h\u1ee3p v\u1edbi c\u00e1c h\u00e0m t\u1ed5ng h\u1ee3p \u0111\u1ec3 t\u00ednh to\u00e1n tr\u00ean t\u1eebng nh\u00f3m.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT column1, aggregate_function(column2)\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM table_name\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">GROUP BY column1;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 t\u00ednh t\u1ed5ng s\u1ed1 ti\u1ec1n \u0111\u01a1n h\u00e0ng c\u1ee7a m\u1ed7i kh\u00e1ch h\u00e0ng, ta d\u00f9ng c\u00fa ph\u00e1p nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT customer_id, SUM(amount)\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM orders\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">GROUP BY customer_id;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u1edbi c\u00fa ph\u00e1p tr\u00ean ta thu \u0111\u01b0\u1ee3c k\u1ebft qu\u1ea3 nh\u01b0 sau:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>customer_id<\/b><\/td>\n<td><b>SUM(amount)<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">400<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">2<\/span><\/td>\n<td><span style=\"font-weight: 400;\">300<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td><span style=\"font-weight: 400;\">450<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span class=\"ez-toc-section\" id=\"SELECT_trong_SQL_voi_menh_de_HAVING\"><\/span><b>SELECT trong SQL v\u1edbi m\u1ec7nh \u0111\u1ec1 HAVING<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">M\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">HAVING<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 l\u1ecdc c\u00e1c nh\u00f3m k\u1ebft qu\u1ea3 sau khi \u0111\u00e3 nh\u00f3m b\u1eb1ng m\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">GROUP BY<\/span><span style=\"font-weight: 400;\">. N\u00f3 t\u01b0\u01a1ng t\u1ef1 nh\u01b0 m\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\">, nh\u01b0ng <\/span><span style=\"font-weight: 400;\">HAVING<\/span><span style=\"font-weight: 400;\"> \u00e1p d\u1ee5ng cho c\u00e1c nh\u00f3m d\u1eef li\u1ec7u thay v\u00ec c\u00e1c b\u1ea3n ghi ri\u00eang l\u1ebb. Having ch\u1ec9 ho\u1ea1t \u0111\u1ed9ng khi c\u00f3 Group By v\u00e0 th\u01b0\u1eddng \u0111\u01b0\u1ee3c k\u1ebft h\u1ee3p v\u1edbi c\u00e1c h\u00e0m t\u1ed5ng h\u1ee3p nh\u01b0 <\/span><span style=\"font-weight: 400;\">COUNT<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">SUM<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">AVG<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">MAX<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">MIN<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT column1, aggregate_function(column2)\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM table_name\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">GROUP BY column1\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">HAVING aggregate_function(column2) condition;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 t\u00ecm nh\u1eefng th\u00e0nh ph\u1ed1 c\u00f3 tu\u1ed5i trung b\u00ecnh c\u1ee7a kh\u00e1ch h\u00e0ng l\u1edbn h\u01a1n 28, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT city, AVG(age) AS average_age\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM customers\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">GROUP BY city\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">HAVING AVG(age) &gt; 28;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u1edbi c\u00fa ph\u00e1p tr\u00ean ta thu \u0111\u01b0\u1ee3c k\u1ebft qu\u1ea3 nh\u01b0 sau:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>city<\/b><\/td>\n<td><b>average_age<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">New York<\/span><\/td>\n<td><span style=\"font-weight: 400;\">29<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Chicago<\/span><\/td>\n<td><span style=\"font-weight: 400;\">35<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span class=\"ez-toc-section\" id=\"Gioi_han_so_luong_ket_qua_voi_LIMIT\"><\/span><b>Gi\u1edbi h\u1ea1n s\u1ed1 l\u01b0\u1ee3ng k\u1ebft qu\u1ea3 v\u1edbi LIMIT<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">M\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">LIMIT<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 gi\u1edbi h\u1ea1n s\u1ed1 l\u01b0\u1ee3ng b\u1ea3n ghi tr\u1ea3 v\u1ec1 t\u1eeb truy v\u1ea5n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT column1, column2, ...\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM table_name\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">LIMIT number_of_rows;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y 2 kh\u00e1ch h\u00e0ng \u0111\u1ea7u ti\u00ean trong b\u1ea3ng, ta d\u00f9ng c\u00fa ph\u00e1p nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT name, city\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM customers\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">LIMIT 2;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u1edbi c\u00fa ph\u00e1p tr\u00ean ta thu \u0111\u01b0\u1ee3c k\u1ebft qu\u1ea3 nh\u01b0 sau:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>name<\/b><\/td>\n<td><b>city<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">John Doe<\/span><\/td>\n<td><span style=\"font-weight: 400;\">New York<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Jane Doe<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Los Angeles<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">Ngo\u00e0i ra, ta c\u00f3 th\u1ec3 k\u1ebft h\u1ee3p LIMIT v\u1edbi OFFSET \u0111\u1ec3 th\u1ef1c hi\u1ec7n ph\u00e2n trang trong truy v\u1ea5n. Trong \u0111\u00f3, LIMIT d\u00f9ng \u0111\u1ec3 gi\u1edbi h\u1ea1n s\u1ed1 l\u01b0\u1ee3ng b\u1ea3n ghi \u0111\u01b0\u1ee3c tr\u1ea3 v\u1ec1, c\u00f2n OFFSET x\u00e1c \u0111\u1ecbnh \u0111i\u1ec3m b\u1eaft \u0111\u1ea7u c\u1ee7a c\u00e1c b\u1ea3n ghi \u0111\u01b0\u1ee3c l\u1ea5y t\u1eeb k\u1ebft qu\u1ea3 v\u1edbi c\u00fa ph\u00e1p nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT column1, column2, ...\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM table_name\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">LIMIT number_of_rows\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">OFFSET number_of_rows_to_skip;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y 2 kh\u00e1ch h\u00e0ng ti\u1ebfp theo (b\u1ecf qua 2 kh\u00e1ch h\u00e0ng \u0111\u1ea7u ti\u00ean) t\u1eeb b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">customers<\/span><\/i><span style=\"font-weight: 400;\">, b\u1ea1n c\u00f3 th\u1ec3 d\u00f9ng truy v\u1ea5n sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT name, city\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM customers\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">LIMIT 2 OFFSET 2;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 ph\u00e2n trang: n\u1ebfu b\u1ea1n c\u00f3 100 b\u1ea3n ghi v\u00e0 mu\u1ed1n hi\u1ec3n th\u1ecb 10 b\u1ea3n ghi m\u1ed7i trang, b\u1ea1n c\u00f3 th\u1ec3 t\u1ea1o truy v\u1ea5n nh\u01b0 sau:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Trang 1:<\/b> <span style=\"font-weight: 400;\">LIMIT 10 OFFSET 0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Trang 2:<\/b> <span style=\"font-weight: 400;\">LIMIT 10 OFFSET 10<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Trang 3:<\/b> <span style=\"font-weight: 400;\">LIMIT 10 OFFSET 20<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\">\u2026<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Ket_hop_du_lieu_tu_nhieu_bang_voi_SELECT_va_JOIN\"><\/span><b>K\u1ebft h\u1ee3p d\u1eef li\u1ec7u t\u1eeb nhi\u1ec1u b\u1ea3ng v\u1edbi SELECT v\u00e0 JOIN<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">L\u1ec7nh <\/span><span style=\"font-weight: 400;\">JOIN<\/span><span style=\"font-weight: 400;\"> cho ph\u00e9p k\u1ebft h\u1ee3p d\u1eef li\u1ec7u t\u1eeb nhi\u1ec1u b\u1ea3ng d\u1ef1a tr\u00ean m\u1ed1i quan h\u1ec7 gi\u1eefa c\u00e1c b\u1ea3ng, th\u01b0\u1eddng l\u00e0 th\u00f4ng qua c\u00e1c kh\u00f3a ngo\u1ea1i.<\/span><\/p>\n<p><b>C\u00fa ph\u00e1p 1<\/b><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT column1, column2, ...\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM table1\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">JOIN table2\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">ON table1.common_column = table2.common_column;<\/span><\/pre>\n<blockquote><p><b>L\u01b0u \u00fd<\/b><span style=\"font-weight: 400;\">: Khi k\u1ebft h\u1ee3p nhi\u1ec1u b\u1ea3ng, n\u1ebfu c\u00f3 c\u1ed9t b\u1ecb tr\u00f9ng t\u00ean gi\u1eefa c\u00e1c b\u1ea3ng th\u00ec c\u1ea7n ph\u1ea3i ch\u1ec9 r\u00f5 c\u1ed9t \u0111\u00f3 thu\u1ed9c b\u1ea3ng n\u00e0o b\u1eb1ng c\u00fa ph\u00e1p <\/span><span style=\"font-weight: 400;\">SELECT table_name.column_name<\/span><span style=\"font-weight: 400;\">.<\/span><\/p><\/blockquote>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 1: \u0111\u1ec3 k\u1ebft h\u1ee3p th\u00f4ng tin kh\u00e1ch h\u00e0ng v\u00e0 \u0111\u01a1n h\u00e0ng c\u1ee7a h\u1ecd, ta d\u00f9ng c\u00fa ph\u00e1p nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT order_id, customers.name, orders.amount\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM customers JOIN orders\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">ON customers.customer_id = orders.customer_id;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u1edbi c\u00fa ph\u00e1p tr\u00ean ta thu \u0111\u01b0\u1ee3c k\u1ebft qu\u1ea3 nh\u01b0 sau:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>order_id<\/strong><\/td>\n<td><strong>name<\/strong><\/td>\n<td><strong>amount<\/strong><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">101<\/span><\/td>\n<td><span style=\"font-weight: 400;\">John Doe<\/span><\/td>\n<td><span style=\"font-weight: 400;\">250<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">104<\/span><\/td>\n<td><span style=\"font-weight: 400;\">John Doe<\/span><\/td>\n<td><span style=\"font-weight: 400;\">150<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">102<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Jane Roe<\/span><\/td>\n<td><span style=\"font-weight: 400;\">300<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">103<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Mike Lee<\/span><\/td>\n<td><span style=\"font-weight: 400;\">450<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 2: \u0111\u1ec3 l\u1ea5y c\u00e1c \u0111\u01a1n h\u00e0ng c\u1ee7a kh\u00e1ch h\u00e0ng \u1edf New York ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT order_id, customers.name, orders.amount\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM customers\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">JOIN orders\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">ON customers.customer_id = orders.customer_id\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE customers.city = 'New York';<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Ngo\u00e0i vi\u1ec7c s\u1eed d\u1ee5ng m\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">JOIN<\/span><span style=\"font-weight: 400;\">, ta c\u0169ng c\u00f3 th\u1ec3 th\u1ef1c hi\u1ec7n k\u1ebft h\u1ee3p hai b\u1ea3ng b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng ph\u00e9p li\u1ec7t k\u00ea c\u00e1c b\u1ea3ng trong m\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">FROM<\/span><span style=\"font-weight: 400;\"> v\u00e0 th\u00eam \u0111i\u1ec1u ki\u1ec7n li\u00ean k\u1ebft trong m\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\"> v\u1edbi c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT column1, column2, ...\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM table1, table2\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE table1.common_column = table2.common_column;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y t\u00ean kh\u00e1ch h\u00e0ng v\u00e0 s\u1ed1 ti\u1ec1n \u0111\u01a1n h\u00e0ng t\u1eeb hai b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">customers<\/span><\/i><span style=\"font-weight: 400;\"> v\u00e0 <\/span><i><span style=\"font-weight: 400;\">orders<\/span><\/i><span style=\"font-weight: 400;\"> m\u00e0 kh\u00f4ng d\u00f9ng <\/span><span style=\"font-weight: 400;\">JOIN<\/span><span style=\"font-weight: 400;\">, ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT customers.name, orders.amount\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM customers, orders\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE customers.customer_id =orders.customer_id;<\/span><\/pre>\n<blockquote><p><span style=\"font-weight: 400;\">Ngo\u00e0i ra b\u1ea1n c\u00f3 th\u1ec3 t\u00ecm hi\u1ec3u th\u00eam v\u1ec1 <a href=\"https:\/\/itviec.com\/blog\/join-trong-sql\/\" target=\"_blank\" rel=\"noopener\"><strong>JOIN trong SQL<\/strong><\/a><\/span><span style=\"font-weight: 400;\">.<\/span><\/p><\/blockquote>\n<h2><span class=\"ez-toc-section\" id=\"Cau_hoi_thuong_gap_ve_SELECT_trong_SQL\"><\/span><b>C\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 SELECT trong SQL<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>S\u1ef1 kh\u00e1c bi\u1ec7t gi\u1eefa WHERE v\u00e0 HAVING l\u00e0 g\u00ec?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">M\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 l\u1ecdc c\u00e1c b\u1ea3n ghi ri\u00eang l\u1ebb. N\u1ebfu trong c\u00e2u truy v\u1ea5n c\u00f3 l\u1ec7nh <\/span><span style=\"font-weight: 400;\">GROUP<\/span> <span style=\"font-weight: 400;\">BY<\/span><span style=\"font-weight: 400;\"> th\u00ec <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\"> s\u1ebd \u0111\u01b0\u1ee3c th\u1ef1c thi tr\u01b0\u1edbc <\/span><span style=\"font-weight: 400;\">GROUP<\/span> <span style=\"font-weight: 400;\">BY<\/span><span style=\"font-weight: 400;\">, trong khi m\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">HAVING<\/span><span style=\"font-weight: 400;\"> l\u1ecdc c\u00e1c nh\u00f3m sau khi h\u00e0m t\u1ed5ng h\u1ee3p \u0111\u01b0\u1ee3c \u00e1p d\u1ee5ng. Ngo\u00e0i ra, <\/span><span style=\"font-weight: 400;\">HAVING<\/span><span style=\"font-weight: 400;\"> c\u1ea7n ph\u1ea3i \u0111i k\u00e8m v\u1edbi <\/span><span style=\"font-weight: 400;\">GROUP BY<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 l\u1ecdc c\u00e1c nh\u00f3m d\u1eef li\u1ec7u, trong khi <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\"> c\u00f3 th\u1ec3 th\u1ef1c thi \u0111\u1ed9c l\u1eadp m\u00e0 kh\u00f4ng c\u1ea7n <\/span><span style=\"font-weight: 400;\">GROUP BY<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<h3><b>Khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng m\u1ec7nh \u0111\u1ec1 HAVING?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">M\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">HAVING<\/span><span style=\"font-weight: 400;\"> n\u00ean \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng khi b\u1ea1n c\u1ea7n l\u1ecdc c\u00e1c nh\u00f3m sau khi \u0111\u00e3 \u00e1p d\u1ee5ng c\u00e1c h\u00e0m t\u1ed5ng h\u1ee3p, ch\u1eb3ng h\u1ea1n nh\u01b0 ch\u1ec9 hi\u1ec3n th\u1ecb nh\u1eefng nh\u00f3m c\u00f3 t\u1ed5ng gi\u00e1 tr\u1ecb l\u1edbn h\u01a1n m\u1ed9t m\u1ee9c nh\u1ea5t \u0111\u1ecbnh.<\/span><\/p>\n<h3><b>C\u00f3 th\u1ec3 s\u1eed d\u1ee5ng nhi\u1ec1u to\u00e1n t\u1eed trong m\u1ed9t c\u00e2u l\u1ec7nh SELECT kh\u00f4ng?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">B\u1ea1n c\u00f3 th\u1ec3 k\u1ebft h\u1ee3p nhi\u1ec1u to\u00e1n t\u1eed trong m\u1ec7nh \u0111\u1ec1 <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 m\u1ed9t truy v\u1ea5n <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\"> b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng c\u00e1c to\u00e1n t\u1eed logic nh\u01b0 <\/span><span style=\"font-weight: 400;\">AND<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">OR<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 t\u1ea1o c\u00e1c \u0111i\u1ec1u ki\u1ec7n l\u1ecdc ph\u1ee9c t\u1ea1p.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Tong_ket_SELECT_trong_SQL\"><\/span><b>T\u1ed5ng k\u1ebft SELECT trong SQL<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">L\u1ec7nh <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\"> \u0111\u00f3ng vai tr\u00f2 thi\u1ebft y\u1ebfu trong SQL, cho ph\u00e9p ng\u01b0\u1eddi d\u00f9ng truy v\u1ea5n v\u00e0 x\u1eed l\u00fd d\u1eef li\u1ec7u m\u1ed9t c\u00e1ch hi\u1ec7u qu\u1ea3. T\u1eeb vi\u1ec7c truy xu\u1ea5t d\u1eef li\u1ec7u \u0111\u01a1n gi\u1ea3n \u0111\u1ebfn c\u00e1c truy v\u1ea5n ph\u1ee9c t\u1ea1p v\u1edbi c\u00e1c m\u1ec7nh \u0111\u1ec1 nh\u01b0 <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">GROUP BY<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">HAVING<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\"> mang l\u1ea1i s\u1ef1 linh ho\u1ea1t trong vi\u1ec7c qu\u1ea3n l\u00fd v\u00e0 ph\u00e2n t\u00edch d\u1eef li\u1ec7u.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">ITviec hy v\u1ecdng b\u00e0i vi\u1ebft tr\u00ean \u0111\u00e3 gi\u00fap b\u1ea1n hi\u1ec3u r\u00f5 c\u00e1c c\u00fa ph\u00e1p v\u00e0 c\u00e1ch \u00e1p d\u1ee5ng c\u00e1c m\u1ec7nh \u0111\u1ec1 kh\u00e1c nhau \u0111\u1ec3 c\u00f3 th\u1ec3 t\u1ed1i \u01b0u h\u00f3a truy v\u1ea5n v\u00e0 x\u1eed l\u00fd d\u1eef li\u1ec7u hi\u1ec7u qu\u1ea3 h\u01a1n.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>L\u1ec7nh SELECT trong SQL l\u00e0 m\u1ed9t c\u00e2u l\u1ec7nh quan tr\u1ecdng v\u00e0 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng ph\u1ed5 bi\u1ebfn nh\u1ea5t \u0111\u1ec3 truy xu\u1ea5t d\u1eef li\u1ec7u t\u1eeb c\u01a1 s\u1edf d\u1eef li\u1ec7u. T\u1eeb nh\u1eefng truy v\u1ea5n \u0111\u01a1n gi\u1ea3n \u0111\u1ec3 l\u1ea5y d\u1eef li\u1ec7u cho \u0111\u1ebfn nh\u1eefng truy v\u1ea5n ph\u1ee9c t\u1ea1p k\u1ebft h\u1ee3p d\u1eef li\u1ec7u t\u1eeb nhi\u1ec1u b\u1ea3ng, SELECT cho ph\u00e9p ng\u01b0\u1eddi [&hellip;]<\/p>\n","protected":false},"author":209,"featured_media":81017,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_gspb_post_css":"","footnotes":""},"categories":[109],"tags":[],"class_list":["post-80860","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chuyen-mon-it"],"blocksy_meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.8 (Yoast SEO v27.7) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>SELECT trong SQL: H\u01b0\u1edbng d\u1eabn c\u00e1c c\u00e1ch \u1ee9ng d\u1ee5ng l\u1ec7nh SELECT - ITviec Blog<\/title>\n<meta name=\"description\" content=\"T\u1ed5ng quan c\u00e1c l\u1ec7nh SELECT trong SQL, t\u1eeb c\u00fa ph\u00e1p c\u01a1 b\u1ea3n \u0111\u1ebfn c\u00e1c m\u1ec7nh \u0111\u1ec1 \u0111i\u1ec1u ki\u1ec7n nh\u01b0 WHERE, HAVING, GROUP BY v\u00e0 JOIN,...\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/itviec.com\/blog\/select-trong-sql\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SELECT trong SQL: H\u01b0\u1edbng d\u1eabn c\u00e1c c\u00e1ch \u1ee9ng d\u1ee5ng l\u1ec7nh SELECT\" \/>\n<meta property=\"og:description\" content=\"L\u1ec7nh SELECT trong SQL l\u00e0 m\u1ed9t c\u00e2u l\u1ec7nh quan tr\u1ecdng v\u00e0 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng ph\u1ed5 bi\u1ebfn nh\u1ea5t \u0111\u1ec3 truy xu\u1ea5t d\u1eef li\u1ec7u t\u1eeb c\u01a1 s\u1edf d\u1eef li\u1ec7u. T\u1eeb nh\u1eefng truy v\u1ea5n \u0111\u01a1n gi\u1ea3n \u0111\u1ec3 l\u1ea5y d\u1eef\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/select-trong-sql\/\" \/>\n<meta property=\"og:site_name\" content=\"ITviec Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ITviec\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:20:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/10\/SELECT-trong-SQL-vippro.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1500\" \/>\n\t<meta property=\"og:image:height\" content=\"790\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"M\u1ef9 Duy\u00ean\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ITviec\" \/>\n<meta name=\"twitter:site\" content=\"@ITviec\" \/>\n<meta name=\"twitter:label1\" content=\"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi\" \/>\n\t<meta name=\"twitter:data1\" content=\"M\u1ef9 Duy\u00ean\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"SELECT trong SQL: H\u01b0\u1edbng d\u1eabn c\u00e1c c\u00e1ch \u1ee9ng d\u1ee5ng l\u1ec7nh SELECT - ITviec Blog","description":"T\u1ed5ng quan c\u00e1c l\u1ec7nh SELECT trong SQL, t\u1eeb c\u00fa ph\u00e1p c\u01a1 b\u1ea3n \u0111\u1ebfn c\u00e1c m\u1ec7nh \u0111\u1ec1 \u0111i\u1ec1u ki\u1ec7n nh\u01b0 WHERE, HAVING, GROUP BY v\u00e0 JOIN,...","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/itviec.com\/blog\/select-trong-sql\/","og_locale":"vi_VN","og_type":"article","og_title":"SELECT trong SQL: H\u01b0\u1edbng d\u1eabn c\u00e1c c\u00e1ch \u1ee9ng d\u1ee5ng l\u1ec7nh SELECT","og_description":"L\u1ec7nh SELECT trong SQL l\u00e0 m\u1ed9t c\u00e2u l\u1ec7nh quan tr\u1ecdng v\u00e0 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng ph\u1ed5 bi\u1ebfn nh\u1ea5t \u0111\u1ec3 truy xu\u1ea5t d\u1eef li\u1ec7u t\u1eeb c\u01a1 s\u1edf d\u1eef li\u1ec7u. T\u1eeb nh\u1eefng truy v\u1ea5n \u0111\u01a1n gi\u1ea3n \u0111\u1ec3 l\u1ea5y d\u1eef","og_url":"https:\/\/itviec.com\/blog\/select-trong-sql\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2024-11-01T09:20:39+00:00","og_image":[{"width":1500,"height":790,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/10\/SELECT-trong-SQL-vippro.jpg","type":"image\/jpeg"}],"author":"M\u1ef9 Duy\u00ean","twitter_card":"summary_large_image","twitter_creator":"@ITviec","twitter_site":"@ITviec","twitter_misc":{"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi":"M\u1ef9 Duy\u00ean","\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc":"12 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/select-trong-sql\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/select-trong-sql\/"},"author":{"name":"M\u1ef9 Duy\u00ean","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/73733c0725c7e39e696a896bd1abe2d7"},"headline":"SELECT trong SQL: H\u01b0\u1edbng d\u1eabn c\u00e1c c\u00e1ch \u1ee9ng d\u1ee5ng l\u1ec7nh SELECT","datePublished":"2024-11-01T09:20:39+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/select-trong-sql\/"},"wordCount":2782,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/select-trong-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/10\/SELECT-trong-SQL-vippro.jpg","articleSection":["Chuy\u00ean m\u00f4n IT"],"inLanguage":"vi"},{"@type":"WebPage","@id":"https:\/\/itviec.com\/blog\/select-trong-sql\/","url":"https:\/\/itviec.com\/blog\/select-trong-sql\/","name":"SELECT trong SQL: H\u01b0\u1edbng d\u1eabn c\u00e1c c\u00e1ch \u1ee9ng d\u1ee5ng l\u1ec7nh SELECT - ITviec Blog","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itviec.com\/blog\/select-trong-sql\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/select-trong-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/10\/SELECT-trong-SQL-vippro.jpg","datePublished":"2024-11-01T09:20:39+00:00","description":"T\u1ed5ng quan c\u00e1c l\u1ec7nh SELECT trong SQL, t\u1eeb c\u00fa ph\u00e1p c\u01a1 b\u1ea3n \u0111\u1ebfn c\u00e1c m\u1ec7nh \u0111\u1ec1 \u0111i\u1ec1u ki\u1ec7n nh\u01b0 WHERE, HAVING, GROUP BY v\u00e0 JOIN,...","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/select-trong-sql\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/select-trong-sql\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/select-trong-sql\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/10\/SELECT-trong-SQL-vippro.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/10\/SELECT-trong-SQL-vippro.jpg","width":1500,"height":790,"caption":"SELECT trong SQL - itviec blog"},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/select-trong-sql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Chuy\u00ean m\u00f4n IT","item":"https:\/\/itviec.com\/blog\/chuyen-mon-it\/"},{"@type":"ListItem","position":2,"name":"SELECT trong SQL: H\u01b0\u1edbng d\u1eabn c\u00e1c c\u00e1ch \u1ee9ng d\u1ee5ng l\u1ec7nh SELECT"}]},{"@type":"WebSite","@id":"https:\/\/itviec.com\/blog\/#website","url":"https:\/\/itviec.com\/blog\/","name":"ITviec Blog","description":"IT Jobs &amp; People in Vietnam","publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/itviec.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"vi"},{"@type":"Organization","@id":"https:\/\/itviec.com\/blog\/#organization","name":"ITviec","url":"https:\/\/itviec.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","width":1800,"height":1800,"caption":"ITviec"},"image":{"@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ITviec","https:\/\/x.com\/ITviec","https:\/\/www.linkedin.com\/company\/itviec","https:\/\/www.youtube.com\/channel\/UCYthAQ3bcGr57M_ag5gHDvQ"]},{"@type":"Person","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/73733c0725c7e39e696a896bd1abe2d7","name":"M\u1ef9 Duy\u00ean","image":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/Author_Duyen-Tran-120x120.jpg","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/Author_Duyen-Tran-120x120.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/Author_Duyen-Tran-120x120.jpg","caption":"M\u1ef9 Duy\u00ean"},"url":"https:\/\/itviec.com\/blog\/author\/my-duyen\/"}]}},"_links":{"self":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/80860","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/users\/209"}],"replies":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/comments?post=80860"}],"version-history":[{"count":0,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/80860\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/81017"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=80860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=80860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=80860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}