{"id":79751,"date":"2024-10-02T08:56:29","date_gmt":"2024-10-02T01:56:29","guid":{"rendered":"https:\/\/itviec.com\/blog\/?p=79751"},"modified":"2024-10-02T08:56:29","modified_gmt":"2024-10-02T01:56:29","slug":"tong-hop-cau-lenh-sql","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/tong-hop-cau-lenh-sql\/","title":{"rendered":"C\u00e2u l\u1ec7nh SQL: T\u1ed5ng h\u1ee3p c\u00e1c c\u00e2u l\u1ec7nh, to\u00e1n t\u1eed v\u00e0 r\u00e0ng bu\u1ed9c SQL"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 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\/tong-hop-cau-lenh-sql\/#SQL_la_gi\" >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\/tong-hop-cau-lenh-sql\/#Cau_lenh_SQL_Ngon_ngu_dinh_nghia_du_lieu_%E2%80%93_DDL_Data_Definition_Language\" >C\u00e2u l\u1ec7nh SQL: Ng\u00f4n ng\u1eef \u0111\u1ecbnh ngh\u0129a d\u1eef li\u1ec7u &#8211; DDL (Data Definition Language)<\/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\/tong-hop-cau-lenh-sql\/#Cau_lenh_SQL_Ngon_ngu_truy_van_du_lieu_%E2%80%93_DQL_Data_Query_Language\" >C\u00e2u l\u1ec7nh SQL: Ng\u00f4n ng\u1eef truy v\u1ea5n d\u1eef li\u1ec7u &#8211; DQL (Data Query Language)<\/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\/tong-hop-cau-lenh-sql\/#Cau_lenh_SQL_Ngon_ngu_thao_tac_du_lieu_%E2%80%93_DML_Data_Manipulation_Language\" >C\u00e2u l\u1ec7nh SQL: Ng\u00f4n ng\u1eef thao t\u00e1c d\u1eef li\u1ec7u &#8211; DML (Data Manipulation Language)<\/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\/tong-hop-cau-lenh-sql\/#Cau_lenh_SQL_Ngon_ngu_dieu_khien_du_lieu_%E2%80%93_DCL_Data_Control_Language\" >C\u00e2u l\u1ec7nh SQL: Ng\u00f4n ng\u1eef \u0111i\u1ec1u khi\u1ec3n d\u1eef li\u1ec7u &#8211; DCL (Data Control Language)<\/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\/tong-hop-cau-lenh-sql\/#Cac_loai_toan_tu_trong_SQL\" >C\u00e1c lo\u1ea1i to\u00e1n t\u1eed trong SQL<\/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\/tong-hop-cau-lenh-sql\/#Nhom_ham_tong_hop\" >Nh\u00f3m 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-8\" href=\"https:\/\/itviec.com\/blog\/tong-hop-cau-lenh-sql\/#Cac_rang_buoc_toan_ven\" >C\u00e1c r\u00e0ng bu\u1ed9c to\u00e0n v\u1eb9n<\/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\/tong-hop-cau-lenh-sql\/#Cac_cau_hoi_thuong_gap_ve_cau_lenh_SQL\" >C\u00e1c c\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 c\u00e2u l\u1ec7nh SQL<\/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\/tong-hop-cau-lenh-sql\/#Tong_ket_Cau_lenh_SQL\" >T\u1ed5ng k\u1ebft C\u00e2u l\u1ec7nh SQL<\/a><\/li><\/ul><\/nav><\/div>\n<p><em><strong>SQL (Structured Query Language) l\u00e0 ng\u00f4n ng\u1eef chu\u1ea9n \u0111\u1ec3 qu\u1ea3n l\u00fd v\u00e0 thao t\u00e1c c\u01a1 s\u1edf d\u1eef li\u1ec7u quan h\u1ec7. T\u1eeb vi\u1ec7c t\u1ea1o b\u1ea3ng, th\u00eam d\u1eef li\u1ec7u \u0111\u1ebfn vi\u1ec7c truy xu\u1ea5t v\u00e0 c\u1eadp nh\u1eadt th\u00f4ng tin, SQL \u0111\u00f3ng vai tr\u00f2 quan tr\u1ecdng trong vi\u1ec7c t\u1ed5 ch\u1ee9c v\u00e0 khai th\u00e1c d\u1eef li\u1ec7u hi\u1ec7u qu\u1ea3. \u0110\u1ed1i v\u1edbi nh\u1eefng ng\u01b0\u1eddi m\u1edbi b\u1eaft \u0111\u1ea7u ho\u1eb7c nh\u1eefng ai \u0111ang t\u00ecm hi\u1ec3u v\u1ec1 c\u01a1 s\u1edf d\u1eef li\u1ec7u, n\u1eafm v\u1eefng c\u00e1c c\u00e2u l\u1ec7nh SQL c\u01a1 b\u1ea3n l\u00e0 b\u01b0\u1edbc kh\u1edfi \u0111\u1ea7u c\u1ea7n thi\u1ebft.<\/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;\">C\u00e1c lo\u1ea1i c\u00e2u l\u1ec7nh SQL<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c to\u00e1n t\u1eed trong SQL<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c lo\u1ea1i r\u00e0ng bu\u1ed9c to\u00e0n v\u1eb9n trong SQL<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"SQL_la_gi\"><\/span><b>SQL l\u00e0 g\u00ec?<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">SQL, vi\u1ebft t\u1eaft c\u1ee7a Structured Query Language, l\u00e0 m\u1ed9t ng\u00f4n ng\u1eef \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf \u0111\u1ec3 t\u01b0\u01a1ng t\u00e1c v\u00e0 qu\u1ea3n l\u00fd d\u1eef li\u1ec7u trong c\u00e1c h\u1ec7 qu\u1ea3n tr\u1ecb c\u01a1 s\u1edf d\u1eef li\u1ec7u quan h\u1ec7 (RDBMS). N\u00f3 cung c\u1ea5p m\u1ed9t c\u00e1ch th\u1ee9c m\u1ea1nh m\u1ebd v\u00e0 linh ho\u1ea1t \u0111\u1ec3 t\u1ea1o, \u0111\u1ecdc, c\u1eadp nh\u1eadt v\u00e0 x\u00f3a d\u1eef li\u1ec7u. B\u00ean c\u1ea1nh \u0111\u00f3, SQL c\u0169ng cho ph\u00e9p ng\u01b0\u1eddi d\u00f9ng th\u1ef1c hi\u1ec7n c\u00e1c truy v\u1ea5n ph\u1ee9c t\u1ea1p \u0111\u1ec3 t\u00ecm ki\u1ebfm v\u00e0 t\u1ed5 ch\u1ee9c d\u1eef li\u1ec7u m\u1ed9t c\u00e1ch hi\u1ec7u qu\u1ea3.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u1edbi s\u1ef1 h\u1ed7 tr\u1ee3 t\u1eeb c\u00e1c h\u1ec7 qu\u1ea3n tr\u1ecb ph\u1ed5 bi\u1ebfn nh\u01b0 MySQL, PostgreSQL, SQL Server,&#8230; SQL \u0111\u00e3 tr\u1edf th\u00e0nh c\u00f4ng c\u1ee5 quan tr\u1ecdng trong l\u0129nh v\u1ef1c ph\u00e1t tri\u1ec3n ph\u1ea7n m\u1ec1m v\u00e0 qu\u1ea3n l\u00fd d\u1eef li\u1ec7u.<\/span><\/p>\n<blockquote><p><em>\u0110\u1ecdc th\u00eam: <a href=\"https:\/\/itviec.com\/blog\/sql-la-gi\/\" target=\"_blank\" rel=\"noopener\"><strong>Truy v\u1ea5n c\u01a1 s\u1edf d\u1eef li\u1ec7u l\u00e0 g\u00ec? T\u1ea5t t\u1ea7n t\u1eadt v\u1ec1 ng\u00f4n ng\u1eef SQL<\/strong><\/a><\/em><\/p><\/blockquote>\n<h2><span class=\"ez-toc-section\" id=\"Cau_lenh_SQL_Ngon_ngu_dinh_nghia_du_lieu_%E2%80%93_DDL_Data_Definition_Language\"><\/span><b>C\u00e2u l\u1ec7nh SQL: Ng\u00f4n ng\u1eef \u0111\u1ecbnh ngh\u0129a d\u1eef li\u1ec7u &#8211; DDL (Data Definition Language)<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">DDL (Data Definition Language) l\u00e0 m\u1ed9t nh\u00f3m c\u00e1c c\u00e2u l\u1ec7nh SQL \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 \u0111\u1ecbnh ngh\u0129a v\u00e0 thay \u0111\u1ed5i c\u1ea5u tr\u00fac c\u1ee7a c\u01a1 s\u1edf d\u1eef li\u1ec7u. N\u00f3i c\u00e1ch kh\u00e1c, DDL t\u1eadp trung v\u00e0o vi\u1ec7c m\u00f4 t\u1ea3 v\u00e0 qu\u1ea3n l\u00fd c\u1ea5u tr\u00fac c\u1ee7a c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng trong c\u01a1 s\u1edf d\u1eef li\u1ec7u nh\u01b0 b\u1ea3ng, ch\u1ec9 m\u1ee5c (index), h\u00e0m, views, th\u1ee7 t\u1ee5c l\u01b0u tr\u1eef v\u00e0 trigger. Tuy nhi\u00ean, DDL kh\u00f4ng x\u1eed l\u00fd tr\u1ef1c ti\u1ebfp d\u1eef li\u1ec7u trong c\u00e1c b\u1ea3ng, m\u00e0 ch\u1ec9 l\u00e0m vi\u1ec7c v\u1edbi c\u1ea5u tr\u00fac c\u1ee7a c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00e1c c\u00e2u l\u1ec7nh DDL th\u01b0\u1eddng kh\u00f4ng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng b\u1edfi ng\u01b0\u1eddi d\u00f9ng th\u00f4ng th\u01b0\u1eddng, m\u00e0 ch\u1ee7 y\u1ebfu d\u00e0nh cho qu\u1ea3n tr\u1ecb vi\u00ean ho\u1eb7c l\u1eadp tr\u00ecnh vi\u00ean c\u00f3 nhi\u1ec7m v\u1ee5 thi\u1ebft k\u1ebf v\u00e0 qu\u1ea3n l\u00fd h\u1ec7 th\u1ed1ng c\u01a1 s\u1edf d\u1eef li\u1ec7u. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 danh s\u00e1ch c\u00e1c c\u00e2u l\u1ec7nh DDL ph\u1ed5 bi\u1ebfn c\u00f9ng v\u1edbi c\u00fa ph\u00e1p:<\/span><\/p>\n<h3><strong>CREATE<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 t\u1ea1o m\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u ho\u1eb7c c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng b\u00ean trong n\u00f3 (b\u1ea3ng, ch\u1ec9 m\u1ee5c, h\u00e0m,&#8230;).<\/span><\/p>\n<p>C\u00fa ph\u00e1p CREATE:<\/p>\n<pre><span style=\"font-weight: 400;\">CREATE <\/span><span style=\"font-weight: 400;\">Object_name (parameters)<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 t\u1ea1o b\u1ea3ng <\/span><span style=\"font-weight: 400;\">employees<\/span><span style=\"font-weight: 400;\"> ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">CREATE TABLE employees (phone_id INT PRIMARY KEY, <\/span>\r\n\r\n<span style=\"font-weight: 400;\">name VARCHAR(50), <\/span>\r\n\r\n<span style=\"font-weight: 400;\">department VARCHAR(50),<\/span>\r\n\r\n<span style=\"font-weight: 400;\">salary DECIMAL(10, 2)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">);<\/span><\/pre>\n<h3><strong>DROP<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 x\u00f3a v\u0129nh vi\u1ec5n c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng trong c\u01a1 s\u1edf d\u1eef li\u1ec7u nh\u01b0 ch\u1ec9 m\u1ee5c, b\u1ea3ng, hay c\u1ea3 c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/p>\n<p>C\u00fa ph\u00e1p DROP:<\/p>\n<pre><span style=\"font-weight: 400;\">DROP TABLE <\/span><span style=\"font-weight: 400;\">[TABLE | DATABASE | INDEX] object_name<\/span><span style=\"font-weight: 400;\">];<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 x\u00f3a b\u1ea3ng <\/span><span style=\"font-weight: 400;\">employees<\/span><span style=\"font-weight: 400;\"> kh\u1ecfi database, ta d\u00f9ng c\u00fa ph\u00e1p sau: <\/span><\/p>\n<pre><span style=\"font-weight: 400;\">DROP TABLE employees;<\/span><\/pre>\n<h3><strong>ALTER<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 thay \u0111\u1ed5i c\u1ea5u tr\u00fac c\u1ee7a b\u1ea3ng, ch\u1eb3ng h\u1ea1n th\u00eam c\u1ed9t (d\u00f9ng ADD), thay \u0111\u1ed5i ki\u1ec3u d\u1eef li\u1ec7u (d\u00f9ng MODIFY), x\u00f3a c\u1ed9t ho\u1eb7c r\u00e0ng bu\u1ed9c (d\u00f9ng DROP) hay \u0111\u1ed5i t\u00ean c\u1ed9t ho\u1eb7c b\u1ea3ng (d\u00f9ng RENAME) m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i x\u00f3a v\u00e0 t\u1ea1o l\u1ea1i b\u1ea3ng.<\/span><\/p>\n<p>C\u00fa ph\u00e1p ALTER:<\/p>\n<pre><span style=\"font-weight: 400;\">ALTER TABLE table_name <\/span>\r\n\r\n<span style=\"font-weight: 400;\">[ADD | MODIFY | DROP| RENAME] column_name [data_type | constraint];<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 th\u00eam th\u00f4ng tin email v\u00e0o b\u1ea3ng <\/span><span style=\"font-weight: 400;\">employees<\/span><span style=\"font-weight: 400;\">, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">ALTER TABLE employees <\/span>\r\n\r\n<span style=\"font-weight: 400;\">ADD COLUMN email VARCHAR(100);<\/span><\/pre>\n<h3><strong>TRUNCATE<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 x\u00f3a to\u00e0n b\u1ed9 d\u1eef li\u1ec7u trong b\u1ea3ng m\u00e0 kh\u00f4ng x\u00f3a c\u1ea5u tr\u00fac b\u1ea3ng. Kh\u00f4ng gian l\u01b0u tr\u1eef c\u1ee7a d\u1eef li\u1ec7u c\u0169ng s\u1ebd \u0111\u01b0\u1ee3c gi\u1ea3i ph\u00f3ng. <\/span><\/p>\n<p>C\u00fa ph\u00e1p TRUNCATE:<\/p>\n<pre><span style=\"font-weight: 400;\">TRUNCATE TABLE table_name;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 x\u00f3a to\u00e0n b\u1ed9 d\u1eef li\u1ec7u trong b\u1ea3ng <\/span><span style=\"font-weight: 400;\">employees<\/span><span style=\"font-weight: 400;\">, nh\u01b0ng b\u1ea3ng v\u00e0 c\u00e1c c\u1ed9t v\u1eabn gi\u1eef nguy\u00ean, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">TRUNCATE TABLE employees;<\/span><\/pre>\n<h3><strong>COMMENT<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">C\u00e2u l\u1ec7nh <\/span><span style=\"font-weight: 400;\">COMMENT<\/span><span style=\"font-weight: 400;\"> d\u00f9ng \u0111\u1ec3 th\u00eam ho\u1eb7c thay \u0111\u1ed5i ghi ch\u00fa (comment) cho m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng trong c\u01a1 s\u1edf d\u1eef li\u1ec7u, gi\u00fap ng\u01b0\u1eddi d\u00f9ng hi\u1ec3u r\u00f5 h\u01a1n v\u1ec1 ch\u1ee9c n\u0103ng c\u1ee7a b\u1ea3ng ho\u1eb7c c\u1ed9t.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">COMMENT ON [TABLE | COLUMN] object_name IS 'comment_text';<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 th\u00eam m\u1ed9t ghi ch\u00fa cho b\u1ea3ng <\/span><span style=\"font-weight: 400;\">employees<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 m\u00f4 t\u1ea3 r\u1eb1ng b\u1ea3ng n\u00e0y l\u01b0u th\u00f4ng tin nh\u00e2n vi\u00ean, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">COMMENT ON TABLE employees IS 'B\u1ea3ng l\u01b0u th\u00f4ng tin nh\u00e2n vi\u00ean';<\/span><\/pre>\n<h3><strong>RENAME<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 \u0111\u1ed5i t\u00ean m\u1ed9t b\u1ea3ng ho\u1eb7c \u0111\u1ed1i t\u01b0\u1ee3ng kh\u00e1c trong c\u01a1 s\u1edf d\u1eef li\u1ec7u. <\/span><\/p>\n<pre><span style=\"font-weight: 400;\">RENAME TABLE old_table_name TO new_table_name;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 \u0111\u1ed5i t\u00ean b\u1ea3ng <\/span><span style=\"font-weight: 400;\">employees<\/span><span style=\"font-weight: 400;\"> th\u00e0nh <\/span><span style=\"font-weight: 400;\">staff<\/span><span style=\"font-weight: 400;\">, c\u00f3 th\u1ec3 \u0111\u1ec3 ph\u00f9 h\u1ee3p h\u01a1n v\u1edbi m\u1ee5c \u0111\u00edch qu\u1ea3n l\u00fd ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">RENAME TABLE employees TO staff;<\/span><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Cau_lenh_SQL_Ngon_ngu_truy_van_du_lieu_%E2%80%93_DQL_Data_Query_Language\"><\/span><b>C\u00e2u l\u1ec7nh SQL: Ng\u00f4n ng\u1eef truy v\u1ea5n d\u1eef li\u1ec7u &#8211; DQL (Data Query Language)<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">DQL (Data Query Language) \u0111\u01b0\u1ee3c xem l\u00e0 m\u1ed9t ph\u1ea7n c\u1ee7a DML, nh\u01b0ng t\u1eadp trung v\u00e0o vi\u1ec7c truy v\u1ea5n v\u00e0 l\u1ea5y d\u1eef li\u1ec7u t\u1eeb c\u01a1 s\u1edf d\u1eef li\u1ec7u. M\u1ee5c \u0111\u00edch ch\u00ednh c\u1ee7a DQL l\u00e0 truy xu\u1ea5t th\u00f4ng tin t\u1eeb c\u00e1c b\u1ea3ng trong c\u01a1 s\u1edf d\u1eef li\u1ec7u d\u1ef1a tr\u00ean \u0111i\u1ec1u ki\u1ec7n m\u00e0 ng\u01b0\u1eddi d\u00f9ng \u0111\u01b0a ra. DQL cho ph\u00e9p l\u1ea5y d\u1eef li\u1ec7u theo y\u00eau c\u1ea7u, \u0111\u1ed3ng th\u1eddi c\u00f3 th\u1ec3 s\u1eafp x\u1ebfp, l\u1ecdc ho\u1eb7c t\u00ednh to\u00e1n d\u1eef li\u1ec7u \u0111\u1ec3 ph\u1ee5c v\u1ee5 cho vi\u1ec7c ph\u00e2n t\u00edch ho\u1eb7c x\u1eed l\u00fd ti\u1ebfp theo.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ch\u1ee9c n\u0103ng c\u1ee7a DQL l\u00e0 l\u1ea5y d\u1eef li\u1ec7u t\u1eeb c\u01a1 s\u1edf d\u1eef li\u1ec7u m\u00e0 kh\u00f4ng l\u00e0m thay \u0111\u1ed5i c\u1ea5u tr\u00fac hay d\u1eef li\u1ec7u g\u1ed1c. K\u1ebft qu\u1ea3 c\u1ee7a c\u00e1c truy v\u1ea5n DQL th\u01b0\u1eddng \u0111\u01b0\u1ee3c hi\u1ec3n th\u1ecb d\u01b0\u1edbi d\u1ea1ng b\u1ea3ng t\u1ea1m th\u1eddi ho\u1eb7c \u0111\u01b0\u1ee3c g\u1eedi \u0111\u1ebfn c\u00e1c ch\u01b0\u01a1ng tr\u00ecnh x\u1eed l\u00fd kh\u00e1c. DQL th\u01b0\u1eddng s\u1eed d\u1ee5ng c\u00e2u l\u1ec7nh ch\u00ednh l\u00e0 <\/span><strong>SELECT<\/strong><span style=\"font-weight: 400;\">, cho ph\u00e9p truy v\u1ea5n d\u1eef li\u1ec7u t\u1eeb m\u1ed9t ho\u1eb7c nhi\u1ec1u b\u1ea3ng, v\u1edbi c\u00e1c \u0111i\u1ec1u ki\u1ec7n c\u1ee5 th\u1ec3:<\/span><\/p>\n<h3><strong>SELECT<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">L\u00e0 c\u00e2u l\u1ec7nh \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng nhi\u1ec1u nh\u1ea5t trong qu\u00e1 tr\u00ecnh ho\u1ea1t \u0111\u1ed9ng v\u1edbi CSDL, d<\/span><span style=\"font-weight: 400;\">\u00f9ng \u0111\u1ec3 l\u1ea5y d\u1eef li\u1ec7u t\u1eeb c\u01a1 s\u1edf d\u1eef li\u1ec7u d\u1ef1a tr\u00ean c\u00e1c \u0111i\u1ec1u ki\u1ec7n \u0111\u01b0\u1ee3c x\u00e1c \u0111\u1ecbnh.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00f3 nhi\u1ec1u lo\u1ea1i <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\"> nh\u01b0 <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\"> kh\u00f4ng \u0111i\u1ec1u ki\u1ec7n, <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\"> c\u00f3 \u0111i\u1ec1u ki\u1ec7n <\/span><span style=\"font-weight: 400;\">WHERE<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\"> v\u1edbi d\u1eef li\u1ec7u k\u1ebft h\u1ee3p t\u1eeb nhi\u1ec1u b\u1ea3ng, <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\"> k\u1ebft h\u1ee3p <\/span><span style=\"font-weight: 400;\">GROUP<\/span> <span style=\"font-weight: 400;\">BY<\/span><span style=\"font-weight: 400;\">,&#8230;<\/span><\/p>\n<p>C\u00fa ph\u00e1p SELECT:<\/p>\n<pre><span style=\"font-weight: 400;\">SELECT column1, column2, ... <\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM table_name <\/span>\r\n\r\n<span style=\"font-weight: 400;\">[WHERE condition]<\/span>\r\n\r\n<span style=\"font-weight: 400;\">[GROUP BY column] [HAVING condition] [ORDER BY column];<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y c\u00e1c th\u00f4ng tin c\u1ee7a nh\u00e2n vi\u00ean \u1edf ph\u00f2ng ban Sales c\u00f3 m\u1ee9c l\u01b0\u01a1ng tr\u00ean 30000, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT name, department, salary <\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM employees <\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE department = 'Sales' AND salary &gt; 30000;<\/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 ph\u00e9p <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_lenh_SQL_Ngon_ngu_thao_tac_du_lieu_%E2%80%93_DML_Data_Manipulation_Language\"><\/span><b>C\u00e2u l\u1ec7nh SQL: Ng\u00f4n ng\u1eef thao t\u00e1c d\u1eef li\u1ec7u &#8211; DML (Data Manipulation Language)<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">DML (Data Manipulation Language) l\u00e0 nh\u00f3m c\u00e1c c\u00e2u l\u1ec7nh SQL d\u00f9ng \u0111\u1ec3 thao t\u00e1c tr\u1ef1c ti\u1ebfp v\u1edbi d\u1eef li\u1ec7u trong c\u01a1 s\u1edf d\u1eef li\u1ec7u. C\u00e1c c\u00e2u l\u1ec7nh n\u00e0y cho ph\u00e9p ng\u01b0\u1eddi d\u00f9ng th\u00eam, s\u1eeda, x\u00f3a d\u1eef li\u1ec7u, c\u0169ng nh\u01b0 th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c kh\u00e1c tr\u00ean d\u1eef li\u1ec7u. DML ch\u1ee7 y\u1ebfu \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 qu\u1ea3n l\u00fd c\u00e1c n\u1ed9i dung d\u1eef li\u1ec7u \u0111\u00e3 \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef m\u00e0 kh\u00f4ng \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn c\u1ea5u tr\u00fac b\u1ea3ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ngo\u00e0i ra, m\u1ed9t s\u1ed1 l\u1ec7nh DML nh\u01b0 <\/span><span style=\"font-weight: 400;\">LOCK<\/span><span style=\"font-weight: 400;\">,<\/span> <span style=\"font-weight: 400;\">CALL<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">EXPLAIN PLAN<\/span><span style=\"font-weight: 400;\"> c\u00f2n cung c\u1ea5p ch\u1ee9c n\u0103ng n\u00e2ng cao trong vi\u1ec7c \u0111i\u1ec1u khi\u1ec3n truy c\u1eadp d\u1eef li\u1ec7u v\u00e0 th\u1ef1c hi\u1ec7n c\u00e1c ch\u01b0\u01a1ng tr\u00ecnh con*.<\/span><\/p>\n<blockquote><p><span style=\"font-weight: 400;\">* <\/span><b>Ch\u01b0\u01a1ng tr\u00ecnh con<\/b><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t kh\u1ed1i m\u00e3 \u0111\u1ed9c l\u1eadp c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c g\u1ecdi \u0111\u1ec3 th\u1ef1c hi\u1ec7n m\u1ed9t t\u00e1c v\u1ee5 c\u1ee5 th\u1ec3, th\u01b0\u1eddng \u0111\u01b0\u1ee3c d\u00f9ng \u0111\u1ec3 t\u00e1i s\u1eed d\u1ee5ng m\u00e3 v\u00e0 gi\u1ea3m thi\u1ec3u l\u1ed7i l\u1eb7p l\u1ea1i trong l\u1eadp tr\u00ecnh.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Trong c\u01a1 s\u1edf d\u1eef li\u1ec7u, ch\u01b0\u01a1ng tr\u00ecnh con th\u01b0\u1eddng l\u00e0 c\u00e1c <\/span><b>th\u1ee7 t\u1ee5c<\/b><span style=\"font-weight: 400;\">(procedure) ho\u1eb7c <\/span><b>h\u00e0m<\/b><span style=\"font-weight: 400;\"> (function) \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef v\u00e0 qu\u1ea3n l\u00fd b\u1edfi h\u1ec7 th\u1ed1ng c\u01a1 s\u1edf d\u1eef li\u1ec7u. Ch\u00fang c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c g\u1ecdi \u0111\u1ec3 th\u1ef1c hi\u1ec7n m\u1ed9t lo\u1ea1t c\u00e1c thao t\u00e1c nh\u01b0 t\u00ednh to\u00e1n, x\u1eed l\u00fd d\u1eef li\u1ec7u ho\u1eb7c th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 ph\u1ee9c t\u1ea1p kh\u00e1c.<\/span><\/p><\/blockquote>\n<p><span style=\"font-weight: 400;\">M\u1ed9t s\u1ed1 l\u1ec7nh DML ph\u1ed5 bi\u1ebfn bao g\u1ed3m:<\/span><\/p>\n<h3><strong>INSERT<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 th\u00eam 1 ho\u1eb7c nhi\u1ec1u h\u00e0ng d\u1eef li\u1ec7u m\u1edbi v\u00e0o b\u1ea3ng. <\/span><\/p>\n<p>C\u00fa ph\u00e1p INSERT:<\/p>\n<pre><span style=\"font-weight: 400;\">INSERT INTO table_name (column1, column2, ...) <\/span><\/pre>\n<p><span style=\"font-weight: 400;\">VALUES (value1, value2, &#8230;);<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 th\u00eam th\u00f4ng tin c\u1ee7a nh\u00e2n vi\u00ean m\u1edbi John Doe v\u00e0o b\u1ea3ng <\/span><span style=\"font-weight: 400;\">employees<\/span><span style=\"font-weight: 400;\">, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/span>\r\n\r\n<span style=\"font-weight: 400;\">INSERT INTO employees (name, department, salary) VALUES ('John Doe', 'Sales', 50000);<\/span><\/pre>\n<h3><strong>UPDATE<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 c\u1eadp nh\u1eadt d\u1eef li\u1ec7u hi\u1ec7n c\u00f3 trong b\u1ea3ng d\u1ef1a tr\u00ean \u0111i\u1ec1u ki\u1ec7n l\u1ecdc.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">UPDATE table_name <\/span>\r\n\r\n<span style=\"font-weight: 400;\">SET column1 = value1, column2 = value2 <\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE condition;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 c\u1eadp nh\u1eadt m\u1ee9c l\u01b0\u01a1ng c\u1ee7a nh\u00e2n vi\u00ean &#8216;John Doe&#8217; l\u00ean 55000 trong b\u1ea3ng <\/span><span style=\"font-weight: 400;\">employees<\/span><span style=\"font-weight: 400;\">, ta d\u00f9ng c\u00fa ph\u00e1p sau: <\/span><\/p>\n<pre><span style=\"font-weight: 400;\">UPDATE employees SET salary = 55000 WHERE name = 'John Doe';<\/span><\/pre>\n<h3><strong>DELETE<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 x\u00f3a 1 ho\u1eb7c nhi\u1ec1u h\u00e0ng d\u1eef li\u1ec7u kh\u1ecfi b\u1ea3ng d\u1ef1a tr\u00ean \u0111i\u1ec1u ki\u1ec7n l\u1ecdc.<\/span><\/p>\n<p>C\u00fa ph\u00e1p DELETE:<\/p>\n<pre><span style=\"font-weight: 400;\">DELETE FROM table_name <\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE condition;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 x\u00f3a b\u1ea3n ghi c\u1ee7a nh\u00e2n vi\u00ean &#8216;John Doe&#8217; kh\u1ecfi b\u1ea3ng <\/span><span style=\"font-weight: 400;\">employees<\/span><span style=\"font-weight: 400;\">, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">DELETE FROM employees <\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE name = 'John Doe';<\/span><\/pre>\n<h3><strong>LOCK<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 kh\u00f3a d\u1eef li\u1ec7u c\u1ee7a b\u1ea3ng trong qu\u00e1 tr\u00ecnh giao d\u1ecbch, ng\u0103n ch\u1eb7n c\u00e1c thao t\u00e1c kh\u00e1c can thi\u1ec7p v\u00e0o d\u1eef li\u1ec7u \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o t\u00ednh to\u00e0n v\u1eb9n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 k\u1ebft h\u1ee3p LOCK v\u1edbi SHARE MODE d\u00f9ng \u0111\u1ec3 kho\u00e1 c\u00e1c giao d\u1ecbch kh\u00e1c \u0111\u1ecdc nh\u01b0ng kh\u00f4ng thay \u0111\u1ed5i d\u1eef li\u1ec7u, EXCLUSIVE MODE d\u00f9ng \u0111\u1ec3 kho\u00e1 ho\u00e0n to\u00e0n, kh\u00f4ng cho ph\u00e9p \u0111\u1ecdc ho\u1eb7c ghi. <\/span><\/p>\n<p>C\u00fa ph\u00e1p LOCK:<\/p>\n<pre><span style=\"font-weight: 400;\">LOCK TABLE table_name IN [SHARE|EXCLUSIVE] MODE;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 kh\u00f3a b\u1ea3ng <\/span><span style=\"font-weight: 400;\">employees<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 ng\u0103n ch\u1eb7n c\u00e1c thao t\u00e1c t\u1eeb ng\u01b0\u1eddi d\u00f9ng kh\u00e1c khi b\u1ea3ng n\u00e0y \u0111ang \u0111\u01b0\u1ee3c ch\u1ec9nh s\u1eeda, ta d\u00f9ng c\u00fa ph\u00e1p sau: <\/span><\/p>\n<pre><span style=\"font-weight: 400;\">LOCK TABLE employees IN EXCLUSIVE MODE;<\/span><\/pre>\n<h3><strong>CALL<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Th\u01b0\u1eddng \u0111\u01b0\u1ee3c d\u00f9ng \u0111\u1ec3 g\u1ecdi m\u1ed9t <\/span>ch\u01b0\u01a1ng tr\u00ecnh con PL\/SQL**<span style=\"font-weight: 400;\">, JAVA\u2026th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong c\u01a1 s\u1edf d\u1eef li\u1ec7u nh\u01b0 <\/span><b>PL\/SQL<\/b><span style=\"font-weight: 400;\"> (Oracle) ho\u1eb7c <\/span><b>T-SQL<\/b><span style=\"font-weight: 400;\"> (SQL Server).<\/span><\/p>\n<blockquote><p><span style=\"font-weight: 400;\">** <\/span><b>PL\/SQL<\/b><b> (Procedural Language\/SQL)<\/b><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh do Oracle ph\u00e1t tri\u1ec3n, \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 m\u1edf r\u1ed9ng kh\u1ea3 n\u0103ng c\u1ee7a SQL trong c\u01a1 s\u1edf d\u1eef li\u1ec7u Oracle.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">PL\/SQL cho ph\u00e9p b\u1ea1n vi\u1ebft c\u00e1c ch\u01b0\u01a1ng tr\u00ecnh con nh\u01b0 th\u1ee7 t\u1ee5c, h\u00e0m v\u00e0 trigger \u0111\u1ec3 t\u1ef1 \u0111\u1ed9ng h\u00f3a v\u00e0 t\u00f9y ch\u1ec9nh c\u00e1c thao t\u00e1c v\u1edbi d\u1eef li\u1ec7u. <\/span><span style=\"font-weight: 400;\">N\u00f3 k\u1ebft h\u1ee3p c\u00e1c y\u1ebfu t\u1ed1 c\u1ee7a l\u1eadp tr\u00ecnh c\u1ea5u tr\u00fac v\u1edbi c\u00e1c l\u1ec7nh SQL, gi\u00fap th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 ph\u1ee9c t\u1ea1p h\u01a1n, nh\u01b0 x\u1eed l\u00fd d\u1eef li\u1ec7u, ki\u1ec3m tra \u0111i\u1ec1u ki\u1ec7n, v\u00e0 th\u1ef1c hi\u1ec7n c\u00e1c ph\u00e9p to\u00e1n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u1edbi PL\/SQL, b\u1ea1n c\u00f3 th\u1ec3 vi\u1ebft c\u00e1c \u0111o\u1ea1n m\u00e3 \u0111\u1ec3 t\u1ef1 \u0111\u1ed9ng h\u00f3a c\u00e1c quy tr\u00ecnh trong c\u01a1 s\u1edf d\u1eef li\u1ec7u v\u00e0 c\u1ea3i thi\u1ec7n hi\u1ec7u su\u1ea5t l\u00e0m vi\u1ec7c v\u1edbi d\u1eef li\u1ec7u.<\/span><\/p><\/blockquote>\n<p>C\u00fa ph\u00e1p CALL:<\/p>\n<pre><span style=\"font-weight: 400;\">CALL procedure_name(arguments);<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 g\u1ecdi ch\u01b0\u01a1ng tr\u00ecnh con <\/span><span style=\"font-weight: 400;\">calculate_bonus<\/span><span style=\"font-weight: 400;\"> v\u1edbi tham s\u1ed1 &#8216;Sales&#8217; \u0111\u1ec3 t\u00ednh to\u00e1n th\u01b0\u1edfng cho nh\u00e2n vi\u00ean trong ph\u00f2ng ban Sales, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">CALL calculate_bonus('Sales');<\/span><\/pre>\n<h3><strong>EXPLAIN PLAN<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 m\u00f4 t\u1ea3 c\u00e1ch truy xu\u1ea5t d\u1eef li\u1ec7u c\u1ee7a m\u1ed9t c\u00e2u l\u1ec7nh SQL. <\/span><\/p>\n<p>C\u00fa ph\u00e1p <span style=\"font-weight: 400;\">EXPLAIN PLAN:<\/span><\/p>\n<pre>EXPLAIN PLAN FOR query_condition;<\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 tr\u00ecnh b\u00e0y l\u1ed9 tr\u00ecnh m\u00e0 c\u01a1 s\u1edf d\u1eef li\u1ec7u s\u1ebd th\u1ef1c hi\u1ec7n \u0111\u1ec3 truy xu\u1ea5t th\u00f4ng tin c\u1ee7a c\u00e1c nh\u00e2n vi\u00ean c\u00f3 m\u1ee9c l\u01b0\u01a1ng tr\u00ean 50000, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p sau: <\/span><\/p>\n<pre><span style=\"font-weight: 400;\">EXPLAIN PLAN FOR SELECT * FROM employees <\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE salary &gt; 50000;<\/span><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Cau_lenh_SQL_Ngon_ngu_dieu_khien_du_lieu_%E2%80%93_DCL_Data_Control_Language\"><\/span><b>C\u00e2u l\u1ec7nh SQL: Ng\u00f4n ng\u1eef \u0111i\u1ec1u khi\u1ec3n d\u1eef li\u1ec7u &#8211; DCL (Data Control Language)<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">DCL l\u00e0 t\u1eadp h\u1ee3p c\u00e1c c\u00e2u l\u1ec7nh SQL li\u00ean quan \u0111\u1ebfn vi\u1ec7c qu\u1ea3n l\u00fd quy\u1ec1n v\u00e0 ki\u1ec3m so\u00e1t truy c\u1eadp d\u1eef li\u1ec7u trong c\u01a1 s\u1edf d\u1eef li\u1ec7u, n\u00f3 cho ph\u00e9p ng\u01b0\u1eddi qu\u1ea3n tr\u1ecb c\u1ea5p ho\u1eb7c thu h\u1ed3i quy\u1ec1n truy c\u1eadp c\u01a1 s\u1edf d\u1eef li\u1ec7u v\u00e0 c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng b\u00ean trong n\u00f3 (b\u1ea3ng, c\u1ed9t, th\u1ee7 t\u1ee5c) \u0111\u1ed1i v\u1edbi ng\u01b0\u1eddi d\u00f9ng ho\u1eb7c 1 nh\u00f3m ng\u01b0\u1eddi d\u00f9ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00e1c l\u1ec7nh DCL ph\u1ed5 bi\u1ebfn g\u1ed3m:<\/span><\/p>\n<h3><strong>GRANT<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 c\u1ea5p quy\u1ec1n cho ng\u01b0\u1eddi d\u00f9ng ho\u1eb7c nh\u00f3m ng\u01b0\u1eddi d\u00f9ng \u0111\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c (nh\u01b0 SELECT, INSERT, UPDATE, DELETE) tr\u00ean c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng trong c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/p>\n<p>C\u00fa ph\u00e1p GRANT:<\/p>\n<pre>GRANT privilege_type ON object_name TO user_name [WITH GRANT OPTION];<\/pre>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 c\u1ea5p quy\u1ec1n SELECT cho ng\u01b0\u1eddi d\u00f9ng user1 \u0111\u1ec3 truy v\u1ea5n b\u1ea3ng employees, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/p>\n<pre>GRANT SELECT ON employees TO user1;<\/pre>\n<h3><strong>REVOKE<\/strong><\/h3>\n<p>D\u00f9ng \u0111\u1ec3 thu h\u1ed3i quy\u1ec1n \u0111\u00e3 c\u1ea5p cho ng\u01b0\u1eddi d\u00f9ng.<\/p>\n<p>C\u00fa ph\u00e1p REVOKE:<\/p>\n<pre>REVOKE privilege_type ON object_name FROM user_name;<\/pre>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 thu h\u1ed3i quy\u1ec1n SELECT c\u1ee7a ng\u01b0\u1eddi d\u00f9ng user1 tr\u00ean b\u1ea3ng employees, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p sau:<\/p>\n<pre>REVOKE SELECT ON employees FROM user1;<\/pre>\n<h3><strong>REVOKE ALL<\/strong><\/h3>\n<p>D\u00f9ng \u0111\u1ec3 thu h\u1ed3i t\u1ea5t c\u1ea3 c\u00e1c quy\u1ec1n \u0111\u00e3 c\u1ea5p cho ng\u01b0\u1eddi d\u00f9ng tr\u00ean m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee5 th\u1ec3.<\/p>\n<p>C\u00fa ph\u00e1p REVOKE ALL:<\/p>\n<pre>REVOKE ALL PRIVILEGES ON object_name FROM user_name;<\/pre>\n<p>V\u00ed d\u1ee5: \u0110\u1ec3 thu h\u1ed3i t\u1ea5t c\u1ea3 c\u00e1c quy\u1ec1n c\u1ee7a ng\u01b0\u1eddi d\u00f9ng user1 tr\u00ean b\u1ea3ng employees, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p sau:<\/p>\n<pre>REVOKE ALL PRIVILEGES ON employees FROM user1;<\/pre>\n<h3><b>C\u00e2u l\u1ec7nh SQL: Ng\u00f4n ng\u1eef ki\u1ec3m so\u00e1t giao d\u1ecbch &#8211; TCL (Transaction Control Language)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">TCL l\u00e0 nh\u00f3m c\u00e1c c\u00e2u l\u1ec7nh d\u00f9ng \u0111\u1ec3 ki\u1ec3m so\u00e1t c\u00e1c giao d\u1ecbch trong c\u01a1 s\u1edf d\u1eef li\u1ec7u. Giao d\u1ecbch l\u00e0 m\u1ed9t nh\u00f3m c\u00e1c thao t\u00e1c \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n nh\u01b0 m\u1ed9t \u0111\u01a1n v\u1ecb th\u1ed1ng nh\u1ea5t; n\u1ebfu b\u1ea5t k\u1ef3 thao t\u00e1c n\u00e0o trong nh\u00f3m b\u1ecb l\u1ed7i, to\u00e0n b\u1ed9 giao d\u1ecbch s\u1ebd b\u1ecb h\u1ee7y b\u1ecf \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o t\u00ednh to\u00e0n v\u1eb9n c\u1ee7a d\u1eef li\u1ec7u.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00e1c l\u1ec7nh TCL ph\u1ed5 bi\u1ebfn g\u1ed3m:<\/span><\/p>\n<h3><strong>BEGIN TRANSACTION<\/strong><\/h3>\n<p>D\u00f9ng \u0111\u1ec3 b\u1eaft \u0111\u1ea7u m\u1ed9t giao d\u1ecbch m\u1edbi.<\/p>\n<p>C\u00fa ph\u00e1p BEGIN TRANSACTION:<\/p>\n<pre>BEGIN TRANSACTION;<\/pre>\n<h3><strong>COMMIT<\/strong><\/h3>\n<p>D\u00f9ng \u0111\u1ec3 x\u00e1c nh\u1eadn v\u00e0 l\u01b0u t\u1ea5t c\u1ea3 c\u00e1c thay \u0111\u1ed5i \u0111\u00e3 th\u1ef1c hi\u1ec7n trong giao d\u1ecbch v\u00e0o c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/p>\n<p>C\u00fa ph\u00e1p COMMIT:<\/p>\n<pre>COMMIT;<\/pre>\n<h3><strong>ROLLBACK<\/strong><\/h3>\n<p>D\u00f9ng \u0111\u1ec3 h\u1ee7y b\u1ecf t\u1ea5t c\u1ea3 c\u00e1c thay \u0111\u1ed5i \u0111\u00e3 th\u1ef1c hi\u1ec7n trong giao d\u1ecbch nh\u01b0ng ch\u01b0a \u0111\u01b0\u1ee3c commit v\u00e0 kh\u00f4i ph\u1ee5c d\u1eef li\u1ec7u v\u1ec1 tr\u1ea1ng th\u00e1i ban \u0111\u1ea7u.<\/p>\n<p>C\u00fa ph\u00e1p ROLLBACK:<\/p>\n<pre>ROLLBACK;<\/pre>\n<h3><strong>SAVEPOINT<\/strong><\/h3>\n<p>D\u00f9ng \u0111\u1ec3 t\u1ea1o m\u1ed9t \u0111i\u1ec3m l\u01b0u trong giao d\u1ecbch \u0111\u1ec3 c\u00f3 th\u1ec3 quay l\u1ea1i n\u1ebfu c\u1ea7n thi\u1ebft.<\/p>\n<p>C\u00fa ph\u00e1p SAVEPOINT:<\/p>\n<pre>SAVEPOINT savepoint_name;<\/pre>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 t\u1ea1o m\u1ed9t \u0111i\u1ec3m l\u01b0u sp1 trong giao d\u1ecbch, cho ph\u00e9p quay l\u1ea1i \u0111i\u1ec3m n\u00e0y n\u1ebfu c\u1ea7n, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/p>\n<pre>SAVEPOINT sp1;<\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 ta c\u00f3:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">-- B\u1eaft \u0111\u1ea7u m\u1ed9t giao d\u1ecbch m\u1edbi<\/span>\r\n<span style=\"font-weight: 400;\">BEGIN TRANSACTION; <\/span>\r\n\r\n<span style=\"font-weight: 400;\">-- Th\u1ef1c hi\u1ec7n m\u1ed9t s\u1ed1 thao t\u00e1c tr\u00ean c\u01a1 s\u1edf d\u1eef li\u1ec7u <\/span>\r\n<span style=\"font-weight: 400;\">INSERT INTO employees (name, department, salary) VALUES ('Alice', 'HR', 60000); <\/span>\r\n<span style=\"font-weight: 400;\">INSERT INTO employees (name, department, salary) VALUES ('Bob', 'IT', 70000); <\/span>\r\n\r\n<span style=\"font-weight: 400;\">-- T\u1ea1o m\u1ed9t \u0111i\u1ec3m l\u01b0u <\/span>\r\n<span style=\"font-weight: 400;\">SAVEPOINT sp1; <\/span>\r\n\r\n<span style=\"font-weight: 400;\">-- Th\u1ef1c hi\u1ec7n th\u00eam thao t\u00e1c kh\u00e1c <\/span>\r\n<span style=\"font-weight: 400;\">INSERT INTO employees (name, department, salary) VALUES ('Charlie', 'Finance', 80000); <\/span>\r\n\r\n<span style=\"font-weight: 400;\">-- Quy\u1ebft \u0111\u1ecbnh kh\u00f4ng mu\u1ed1n gi\u1eef thay \u0111\u1ed5i cho thao t\u00e1c cu\u1ed1i c\u00f9ng <\/span>\r\n<span style=\"font-weight: 400;\">ROLLBACK TO sp1; <\/span>\r\n\r\n<span style=\"font-weight: 400;\">-- K\u1ebft th\u00fac giao d\u1ecbch v\u00e0 l\u01b0u c\u00e1c thay \u0111\u1ed5i \u0111\u00e3 th\u1ef1c hi\u1ec7n tr\u01b0\u1edbc \u0111i\u1ec3m l\u01b0u<\/span>\r\n<span style=\"font-weight: 400;\">COMMIT; <\/span>\r\n\r\n<span style=\"font-weight: 400;\">-- Ho\u1eb7c n\u1ebfu b\u1ea1n mu\u1ed1n h\u1ee7y b\u1ecf to\u00e0n b\u1ed9 giao d\u1ecbch <\/span>\r\n<span style=\"font-weight: 400;\">-- ROLLBACK;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">\u0110o\u1ea1n code tr\u00ean m\u00f4 t\u1ea3 quy tr\u00ecnh s\u1eed d\u1ee5ng giao d\u1ecbch trong SQL \u0111\u1ec3 qu\u1ea3n l\u00fd v\u00e0 \u0111i\u1ec1u ch\u1ec9nh c\u00e1c thay \u0111\u1ed5i \u0111\u1ed1i v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Quy tr\u00ecnh b\u1eaft \u0111\u1ea7u v\u1edbi m\u1ed9t giao d\u1ecbch, th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c d\u1eef li\u1ec7u, v\u00e0 t\u1ea1o m\u1ed9t \u0111i\u1ec3m l\u01b0u <\/span><i><span style=\"font-weight: 400;\">SAVEPOINT<\/span><\/i><span style=\"font-weight: 400;\"> \u0111\u1ec3 c\u00f3 th\u1ec3 quay l\u1ea1i tr\u1ea1ng th\u00e1i tr\u01b0\u1edbc \u0111\u00f3 n\u1ebfu c\u1ea7n.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">N\u1ebfu ph\u00e1t hi\u1ec7n l\u1ed7i ho\u1eb7c kh\u00f4ng mu\u1ed1n gi\u1eef c\u00e1c thay \u0111\u1ed5i sau \u0111i\u1ec3m l\u01b0u, l\u1ec7nh <\/span><i><span style=\"font-weight: 400;\">ROLLBACK TO<\/span><\/i><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 h\u1ee7y b\u1ecf c\u00e1c thay \u0111\u1ed5i kh\u00f4ng mong mu\u1ed1n.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Cu\u1ed1i c\u00f9ng, <\/span><i><span style=\"font-weight: 400;\">COMMIT<\/span><\/i><span style=\"font-weight: 400;\"> l\u01b0u l\u1ea1i t\u1ea5t c\u1ea3 c\u00e1c thay \u0111\u1ed5i h\u1ee3p l\u1ec7 v\u00e0o c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Cac_loai_toan_tu_trong_SQL\"><\/span><b>C\u00e1c lo\u1ea1i to\u00e1n t\u1eed trong SQL<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">To\u00e1n t\u1eed (Operator) trong SQL l\u00e0 c\u00e1c k\u00fd hi\u1ec7u ho\u1eb7c t\u1eeb kh\u00f3a d\u00f9ng \u0111\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c ph\u00e9p t\u00ednh tr\u00ean d\u1eef li\u1ec7u v\u00e0 tr\u1ea3 v\u1ec1 k\u1ebft qu\u1ea3 mong mu\u1ed1n. To\u00e1n t\u1eed \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong c\u00e1c c\u00e2u l\u1ec7nh SQL \u0111\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c ph\u00e9p to\u00e1n s\u1ed1 h\u1ecdc, so s\u00e1nh gi\u00e1 tr\u1ecb, k\u1ebft h\u1ee3p \u0111i\u1ec1u ki\u1ec7n logic v\u00e0 nhi\u1ec1u thao t\u00e1c kh\u00e1c. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 c\u00e1c lo\u1ea1i to\u00e1n t\u1eed ph\u1ed5 bi\u1ebfn trong SQL:<\/span><\/p>\n<h3><b>SQL Arithmetic Operators (To\u00e1n t\u1eed s\u1ed1 h\u1ecdc)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">To\u00e1n t\u1eed s\u1ed1 h\u1ecdc \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c ph\u00e9p t\u00ednh nh\u01b0 c\u1ed9ng, tr\u1eeb, nh\u00e2n, chia tr\u00ean c\u00e1c c\u1ed9t ho\u1eb7c c\u00e1c gi\u00e1 tr\u1ecb s\u1ed1. C\u00e1c to\u00e1n t\u1eed thu\u1ed9c nh\u00f3m n\u00e0y bao g\u1ed3m:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>To\u00e1n t\u1eed<\/b><\/td>\n<td><b>Ph\u00e9p to\u00e1n<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">+<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ph\u00e9p c\u1ed9ng<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&#8211;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ph\u00e9p tr\u1eeb<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">*<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ph\u00e9p nh\u00e2n<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">\/<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ph\u00e9p chia<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">%<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ph\u00e9p chia l\u1ea5y d\u01b0<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5: \u0110\u1ec3 c\u1ed9ng th\u00eam ti\u1ec1n l\u01b0\u01a1ng <\/span><i><span style=\"font-weight: 400;\">Salary<\/span><\/i><span style=\"font-weight: 400;\"> cho nh\u00e2n vi\u00ean <\/span><i><span style=\"font-weight: 400;\">John Doe<\/span><\/i> <span style=\"font-weight: 400;\">th\u00eam 10000, ta d\u00f9ng c\u00fa ph\u00e1p sau<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">UPDATE employees <\/span>\r\n\r\n<span style=\"font-weight: 400;\">SET salary = salary + 10000 <\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE name = 'John Doe';<\/span><\/pre>\n<h3><b>SQL Comparison Operators (To\u00e1n t\u1eed so s\u00e1nh)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">To\u00e1n t\u1eed so s\u00e1nh d\u00f9ng \u0111\u1ec3 so s\u00e1nh hai gi\u00e1 tr\u1ecb v\u00e0 tr\u1ea3 v\u1ec1 k\u1ebft qu\u1ea3 l\u00e0 TRUE (\u0111\u00fang) ho\u1eb7c FALSE (sai). To\u00e1n t\u1eed so s\u00e1nh g\u1ed3m c\u00e1c ph\u00e9p to\u00e1n sau:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>To\u00e1n t\u1eed<\/b><\/td>\n<td><b>Ph\u00e9p to\u00e1n<\/b><\/td>\n<td><b>M\u00f4 t\u1ea3<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">=<\/span><\/td>\n<td><span style=\"font-weight: 400;\">B\u1eb1ng<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ki\u1ec3m tra n\u1ebfu c\u1ea3 hai to\u00e1n h\u1ea1ng c\u00f3 gi\u00e1 tr\u1ecb b\u1eb1ng nhau, n\u1ebfu \u0111\u00fang s\u1ebd tr\u1ea3 v\u1ec1 TRUE.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&gt; <\/span><\/td>\n<td><span style=\"font-weight: 400;\">L\u1edbn h\u01a1n<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Tr\u1ea3 v\u1ec1 TRUE n\u1ebfu gi\u00e1 tr\u1ecb b\u00ean tr\u00e1i l\u1edbn h\u01a1n gi\u00e1 tr\u1ecb b\u00ean ph\u1ea3i. Ng\u01b0\u1ee3c l\u1ea1i s\u1ebd tr\u1ea3 v\u1ec1 FALSE.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&lt; <\/span><\/td>\n<td><span style=\"font-weight: 400;\">Nh\u1ecf h\u01a1n<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Tr\u1ea3 v\u1ec1 TRUE n\u1ebfu gi\u00e1 tr\u1ecb b\u00ean tr\u00e1i nh\u1ecf h\u01a1n gi\u00e1 tr\u1ecb b\u00ean ph\u1ea3i. Ng\u01b0\u1ee3c l\u1ea1i s\u1ebd tr\u1ea3 v\u1ec1 FALSE.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&gt;=<\/span><\/td>\n<td><span style=\"font-weight: 400;\">L\u1edbn h\u01a1n ho\u1eb7c b\u1eb1ng<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ki\u1ec3m tra n\u1ebfu gi\u00e1 tr\u1ecb b\u00ean tr\u00e1i l\u1edbn h\u01a1n ho\u1eb7c b\u1eb1ng b\u00ean ph\u1ea3i, n\u1ebfu \u0111\u00fang tr\u1ea3 v\u1ec1 TRUE.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&lt;=<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Nh\u1ecf h\u01a1n ho\u1eb7c b\u1eb1ng<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ki\u1ec3m tra gi\u00e1 tr\u1ecb b\u00ean tr\u00e1i c\u00f3 nh\u1ecf h\u01a1n ho\u1eb7c b\u1eb1ng gi\u00e1 tr\u1ecb b\u00ean ph\u1ea3i hay kh\u00f4ng.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&lt;&gt;<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">!=<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Kh\u00e1c nhau<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ki\u1ec3m tra n\u1ebfu gi\u00e1 tr\u1ecb hai b\u00ean to\u00e1n t\u1eed kh\u00f4ng b\u1eb1ng nhau, n\u1ebfu \u0111\u00fang s\u1ebd tr\u1ea3 v\u1ec1 TRUE.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">!&gt;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Kh\u00f4ng l\u1edbn h\u01a1n<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ki\u1ec3m tra n\u1ebfu gi\u00e1 tr\u1ecb b\u00ean tr\u00e1i kh\u00f4ng l\u1edbn h\u01a1n ho\u1eb7c gi\u00e1 tr\u1ecb b\u00ean ph\u1ea3i.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">!&lt;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Kh\u00f4ng nh\u1ecf h\u01a1n<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ki\u1ec3m tra n\u1ebfu gi\u00e1 tr\u1ecb b\u00ean tr\u00e1i kh\u00f4ng nh\u1ecf h\u01a1n ho\u1eb7c b\u1eb1ng gi\u00e1 tr\u1ecb b\u00ean ph\u1ea3i.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y th\u00f4ng tin c\u00e1c nh\u00e2n vi\u00ean c\u00f3 m\u1ee9c l\u01b0\u01a1ng Salary nhi\u1ec1u h\u01a1n 30000, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT * <\/span>\r\n\r\n<span style=\"font-weight: 400;\">FROM nhanvien <\/span>\r\n\r\n<span style=\"font-weight: 400;\">WHERE luong &gt; 30000;<\/span><\/pre>\n<h3><b>SQL Logical Operators (To\u00e1n t\u1eed logic)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">To\u00e1n t\u1eed logic trong SQL l\u00e0 nh\u1eefng to\u00e1n t\u1eed \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 k\u1ebft h\u1ee3p c\u00e1c \u0111i\u1ec1u ki\u1ec7n ho\u1eb7c bi\u1ec3u th\u1ee9c trong c\u00e2u l\u1ec7nh truy v\u1ea5n. Ch\u00fang gi\u00fap x\u00e1c \u0111\u1ecbnh k\u1ebft qu\u1ea3 d\u1ef1a tr\u00ean c\u00e1c \u0111i\u1ec1u ki\u1ec7n \u0111\u01b0\u1ee3c ch\u1ec9 \u0111\u1ecbnh, tr\u1ea3 v\u1ec1 TRUE ho\u1eb7c FALSE.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00e1c ph\u00e9p to\u00e1n t\u1eed bao g\u1ed3m:<\/span><\/p>\n<h4><strong>ALL<\/strong><\/h4>\n<p>So s\u00e1nh m\u1ed9t gi\u00e1 tr\u1ecb v\u1edbi t\u1ea5t c\u1ea3 c\u00e1c gi\u00e1 tr\u1ecb kh\u00e1c trong m\u1ed9t t\u1eadp d\u1eef li\u1ec7u. N\u1ebfu \u0111i\u1ec1u ki\u1ec7n \u0111\u00fang v\u1edbi m\u1ecdi gi\u00e1 tr\u1ecb trong t\u1eadp, k\u1ebft qu\u1ea3 tr\u1ea3 v\u1ec1 l\u00e0 TRUE.<\/p>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y th\u00f4ng tin nh\u00e2n vi\u00ean c\u00f3 m\u1ee9c l\u01b0\u01a1ng cao h\u01a1n t\u1ea5t c\u1ea3 c\u00e1c nh\u00e2n vi\u00ean trong b\u1ed9 ph\u1eadn IT, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/p>\n<pre>SELECT *\r\nFROM employees\r\nWHERE salary &gt; ALL (SELECT salary FROM employees WHERE department = 'IT');<\/pre>\n<h4><strong>AND<\/strong><\/h4>\n<p>Tr\u1ea3 v\u1ec1 TRUE n\u1ebfu t\u1ea5t c\u1ea3 c\u00e1c \u0111i\u1ec1u ki\u1ec7n trong c\u00e2u l\u1ec7nh \u0111\u1ec1u \u0111\u00fang.<\/p>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y th\u00f4ng tin nh\u00e2n vi\u00ean c\u00f3 l\u01b0\u01a1ng l\u1edbn h\u01a1n 30000 v\u00e0 thu\u1ed9c b\u1ed9 ph\u1eadn IT, ta d\u00f9ng c\u00fa ph\u00e1p:<\/p>\n<pre>SELECT *\r\nFROM employees\r\nWHERE salary &gt; 30000 AND department = 'IT';<\/pre>\n<h4><strong>OR<\/strong><\/h4>\n<p>Tr\u1ea3 v\u1ec1 TRUE n\u1ebfu \u00edt nh\u1ea5t m\u1ed9t trong c\u00e1c \u0111i\u1ec1u ki\u1ec7n \u0111\u01b0\u1ee3c ng\u0103n c\u00e1ch b\u1edfi OR l\u00e0 \u0111\u00fang.<\/p>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y th\u00f4ng tin nh\u00e2n vi\u00ean thu\u1ed9c b\u1ed9 ph\u1eadn IT ho\u1eb7c c\u00f3 m\u1ee9c l\u01b0\u01a1ng l\u1edbn h\u01a1n 30000, ta d\u00f9ng c\u00fa ph\u00e1p:<\/p>\n<pre>SELECT *\r\nFROM employees\r\nWHERE department = 'IT' OR salary &gt; 30000;<\/pre>\n<h4><strong>ANY<\/strong><\/h4>\n<p>So s\u00e1nh m\u1ed9t gi\u00e1 tr\u1ecb c\u1ee5 th\u1ec3 v\u1edbi b\u1ea5t k\u1ef3 gi\u00e1 tr\u1ecb n\u00e0o trong m\u1ed9t t\u1eadp d\u1eef li\u1ec7u. Tr\u1ea3 v\u1ec1 TRUE n\u1ebfu \u0111i\u1ec1u ki\u1ec7n \u0111\u00fang v\u1edbi \u00edt nh\u1ea5t m\u1ed9t gi\u00e1 tr\u1ecb trong t\u1eadp.<\/p>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y th\u00f4ng tin nh\u00e2n vi\u00ean c\u00f3 m\u1ee9c l\u01b0\u01a1ng cao h\u01a1n \u00edt nh\u1ea5t m\u1ed9t nh\u00e2n vi\u00ean trong b\u1ed9 ph\u1eadn IT:<\/p>\n<pre>SELECT *\r\nFROM employees\r\nWHERE salary &gt; ANY (SELECT salary FROM employees WHERE department = 'IT');<\/pre>\n<h4><strong>SOME<\/strong><\/h4>\n<p>T\u01b0\u01a1ng t\u1ef1 nh\u01b0 ANY, to\u00e1n t\u1eed n\u00e0y d\u00f9ng \u0111\u1ec3 so s\u00e1nh m\u1ed9t gi\u00e1 tr\u1ecb v\u1edbi m\u1ed9t t\u1eadp v\u00e0 tr\u1ea3 v\u1ec1 TRUE n\u1ebfu \u0111i\u1ec1u ki\u1ec7n \u0111\u00fang v\u1edbi \u00edt nh\u1ea5t m\u1ed9t gi\u00e1 tr\u1ecb trong t\u1eadp.<\/p>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y th\u00f4ng tin c\u00e1c nh\u00e2n vi\u00ean c\u00f3 m\u1ee9c l\u01b0\u01a1ng cao h\u01a1n \u00edt nh\u1ea5t m\u1ed9t nh\u00e2n vi\u00ean trong b\u1ed9 ph\u1eadn nh\u00e2n s\u1ef1, ta c\u00f3 th\u1ec3 d\u00f9ng c\u00fa ph\u00e1p sau:<\/p>\n<pre>SELECT *\r\nFROM employees\r\nWHERE salary &gt; SOME (SELECT salary FROM employees WHERE department = 'HR');<\/pre>\n<h4><strong>LIKE<\/strong><\/h4>\n<p>T\u00ecm c\u00e1c h\u00e0ng c\u00f3 d\u1eef li\u1ec7u kh\u1edbp v\u1edbi m\u1ed9t m\u1eabu k\u00fd t\u1ef1 c\u1ee5 th\u1ec3. Th\u01b0\u1eddng d\u00f9ng v\u1edbi k\u00fd t\u1ef1 \u0111\u1ea1i di\u1ec7n nh\u01b0 % ho\u1eb7c _.<\/p>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y th\u00f4ng tin c\u00e1c nh\u00e2n vi\u00ean c\u00f3 t\u00ean b\u1eaft \u0111\u1ea7u b\u1eb1ng ch\u1eef &#8220;A&#8221;, ta d\u00f9ng c\u00fa ph\u00e1p:<\/p>\n<pre>SELECT *\r\nFROM employees\r\nWHERE name LIKE 'A%';<\/pre>\n<h4><strong>IN<\/strong><\/h4>\n<p>Ki\u1ec3m tra n\u1ebfu m\u1ed9t gi\u00e1 tr\u1ecb c\u00f3 n\u1eb1m trong m\u1ed9t danh s\u00e1ch c\u00e1c gi\u00e1 tr\u1ecb \u0111\u01b0\u1ee3c ch\u1ec9 \u0111\u1ecbnh hay kh\u00f4ng. Tr\u1ea3 v\u1ec1 TRUE n\u1ebfu c\u00f3.<\/p>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y th\u00f4ng tin nh\u00e2n vi\u00ean thu\u1ed9c b\u1ed9 ph\u1eadn IT ho\u1eb7c HR:<\/p>\n<pre>SELECT *\r\nFROM employees\r\nWHERE department IN ('IT', 'HR');<\/pre>\n<h4><strong>BETWEEN-AND<\/strong><\/h4>\n<p>Ki\u1ec3m tra m\u1ed9t gi\u00e1 tr\u1ecb n\u1eb1m trong m\u1ed9t kho\u1ea3ng gi\u00e1 tr\u1ecb (bao g\u1ed3m c\u1ea3 gi\u00e1 tr\u1ecb b\u1eaft \u0111\u1ea7u v\u00e0 k\u1ebft th\u00fac c\u1ee7a kho\u1ea3ng) hay kh\u00f4ng.<\/p>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y th\u00f4ng tin nh\u00e2n vi\u00ean c\u00f3 m\u1ee9c l\u01b0\u01a1ng n\u1eb1m trong kho\u1ea3ng t\u1eeb 20000 \u0111\u1ebfn 40000, ta d\u00f9ng c\u00fa ph\u00e1p:<\/p>\n<pre>SELECT *\r\nFROM employees\r\nWHERE salary BETWEEN 20000 AND 40000;<\/pre>\n<h4><strong>NOT<\/strong><\/h4>\n<p>\u0110\u1ea3o ng\u01b0\u1ee3c k\u1ebft qu\u1ea3 c\u1ee7a m\u1ed9t \u0111i\u1ec1u ki\u1ec7n. N\u1ebfu \u0111i\u1ec1u ki\u1ec7n tr\u1ea3 v\u1ec1 TRUE, th\u00ec khi d\u00f9ng NOT s\u1ebd tr\u1ea3 v\u1ec1 FALSE v\u00e0 ng\u01b0\u1ee3c l\u1ea1i.<\/p>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y th\u00f4ng tin c\u00e1c nh\u00e2n vi\u00ean kh\u00f4ng thu\u1ed9c b\u1ed9 ph\u1eadn IT, ta d\u00f9ng c\u00fa ph\u00e1p:<\/p>\n<pre>SELECT *\r\nFROM employees\r\nWHERE NOT department = 'IT';<\/pre>\n<h4><strong>EXIST<\/strong><\/h4>\n<p>Ki\u1ec3m tra s\u1ef1 t\u1ed3n t\u1ea1i c\u1ee7a m\u1ed9t b\u1ea3n ghi trong m\u1ed9t t\u1eadp k\u1ebft qu\u1ea3 con (subquery). Tr\u1ea3 v\u1ec1 TRUE n\u1ebfu subquery tr\u1ea3 v\u1ec1 \u00edt nh\u1ea5t m\u1ed9t h\u00e0ng.<\/p>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y th\u00f4ng tin c\u00e1c nh\u00e2n vi\u00ean trong b\u1ea3ng employees m\u00e0 c\u00f3 t\u1ed3n t\u1ea1i trong b\u1ea3ng managers, ta c\u00f3 th\u1ec3 d\u00f9ng c\u00fa ph\u00e1p sau:<\/p>\n<pre>SELECT *\r\nFROM employees e\r\nWHERE EXISTS (SELECT 1 FROM managers m WHERE m.employee_id = e.employee_id);<\/pre>\n<h4><strong>IS NULL<\/strong><\/h4>\n<p>Tr\u1ea3 v\u1ec1 TRUE n\u1ebfu gi\u00e1 tr\u1ecb c\u1ee7a m\u1ed9t tr\u01b0\u1eddng l\u00e0 NULL, t\u1ee9c l\u00e0 kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb.<\/p>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y th\u00f4ng tin nh\u00e2n vi\u00ean ch\u01b0a c\u00f3 th\u00f4ng tin v\u1ec1 s\u1ed1 \u0111i\u1ec7n tho\u1ea1i (phone_number l\u00e0 NULL), ta d\u00f9ng c\u00fa ph\u00e1p:<\/p>\n<pre>SELECT *\r\nFROM employees\r\nWHERE phone_number IS NULL;<\/pre>\n<h3><b>SQL Set Operators (To\u00e1n t\u1eed t\u1eadp h\u1ee3p)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">L\u00e0 c\u00e1c to\u00e1n t\u1eed d\u00f9ng \u0111\u1ec3 k\u1ebft h\u1ee3p k\u1ebft qu\u1ea3 c\u1ee7a nhi\u1ec1u c\u00e2u truy v\u1ea5n <\/span><span style=\"font-weight: 400;\">SELECT<\/span><span style=\"font-weight: 400;\">. C\u00e1c to\u00e1n t\u1eed n\u00e0y thao t\u00e1c tr\u00ean c\u00e1c t\u1eadp h\u1ee3p d\u1eef li\u1ec7u v\u00e0 tr\u1ea3 v\u1ec1 k\u1ebft qu\u1ea3 d\u01b0\u1edbi d\u1ea1ng m\u1ed9t t\u1eadp h\u1ee3p m\u1edbi, c\u00f3 th\u1ec3 bao g\u1ed3m c\u00e1c b\u1ea3n ghi duy nh\u1ea5t ho\u1eb7c gi\u1eef nguy\u00ean c\u00e1c b\u1ea3n ghi tr\u00f9ng l\u1eb7p. Set operators th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 so s\u00e1nh v\u00e0 x\u1eed l\u00fd c\u00e1c t\u1eadp d\u1eef li\u1ec7u t\u1eeb nhi\u1ec1u b\u1ea3ng kh\u00e1c nhau.<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>To\u00e1n t\u1eed<\/b><\/td>\n<td><b>Ph\u00e9p to\u00e1n<\/b><\/td>\n<td><b>M\u00f4 t\u1ea3<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">UNION<\/span><\/td>\n<td><span style=\"font-weight: 400;\">K\u1ebft h\u1ee3p<\/span><\/td>\n<td><span style=\"font-weight: 400;\">K\u1ebft h\u1ee3p k\u1ebft qu\u1ea3 t\u1eeb hai ho\u1eb7c nhi\u1ec1u c\u00e2u truy v\u1ea5n v\u00e0 lo\u1ea1i b\u1ecf c\u00e1c b\u1ea3n ghi tr\u00f9ng l\u1eb7p.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">UNION ALL<\/span><\/td>\n<td><span style=\"font-weight: 400;\">K\u1ebft h\u1ee3p (gi\u1eef tr\u00f9ng l\u1eb7p)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">K\u1ebft h\u1ee3p k\u1ebft qu\u1ea3 t\u1eeb hai ho\u1eb7c nhi\u1ec1u c\u00e2u truy v\u1ea5n v\u00e0 <\/span><b>gi\u1eef nguy\u00ean c\u00e1c b\u1ea3n ghi tr\u00f9ng l\u1eb7p<\/b><span style=\"font-weight: 400;\">.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">INTERSECT<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ph\u00e9p giao<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Tr\u1ea3 v\u1ec1 c\u00e1c b\u1ea3n ghi <\/span><b>chung<\/b><span style=\"font-weight: 400;\"> gi\u1eefa hai ho\u1eb7c nhi\u1ec1u t\u1eadp k\u1ebft qu\u1ea3. Kh\u00f4ng h\u1ed7 tr\u1ee3 trong MySQL.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">MINUS\/EXCEPT<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ph\u00e9p hi\u1ec7u<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Tr\u1ea3 v\u1ec1 c\u00e1c b\u1ea3n ghi c\u00f3 trong t\u1eadp k\u1ebft qu\u1ea3 c\u1ee7a c\u00e2u truy v\u1ea5n \u0111\u1ea7u ti\u00ean <\/span><b>nh\u01b0ng kh\u00f4ng c\u00f3<\/b><span style=\"font-weight: 400;\"> trong c\u00e2u truy v\u1ea5n th\u1ee9 hai. EXCEPT d\u00f9ng trong SQL Server c\u00f2n MINUS s\u1eed d\u1ee5ng trong Oracle.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5: \u0111\u1ec3 l\u1ea5y danh s\u00e1ch t\u00ean t\u1eeb b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">employees<\/span><\/i><span style=\"font-weight: 400;\"> v\u00e0 <\/span><i><span style=\"font-weight: 400;\">managers<\/span><\/i><span style=\"font-weight: 400;\"> m\u00e0 lo\u1ea1i b\u1ecf c\u00e1c t\u00ean tr\u00f9ng l\u1eb7p, ta s\u1eed d\u1ee5ng to\u00e1n t\u1eed <\/span><span style=\"font-weight: 400;\">UNION<\/span><span style=\"font-weight: 400;\"> v\u1edbi c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT name FROM employees <\/span>\r\n\r\n<span style=\"font-weight: 400;\">UNION <\/span>\r\n\r\n<span style=\"font-weight: 400;\">SELECT name FROM managers;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5: \u0111\u1ec3 l\u1ea5y danh s\u00e1ch t\u00ean nh\u00e2n vi\u00ean ch\u1ec9 xu\u1ea5t hi\u1ec7n \u0111\u1ed3ng th\u1eddi trong c\u1ea3 b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">employees<\/span><\/i><span style=\"font-weight: 400;\"> v\u00e0 <\/span><i><span style=\"font-weight: 400;\">managers<\/span><\/i><span style=\"font-weight: 400;\">,ta s\u1eed d\u1ee5ng to\u00e1n t\u1eed <\/span><span style=\"font-weight: 400;\">INTERSECT<\/span><span style=\"font-weight: 400;\"> c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT name FROM employees <\/span>\r\n\r\n<span style=\"font-weight: 400;\">INTERSECT <\/span>\r\n\r\n<span style=\"font-weight: 400;\">SELECT name FROM managers;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5: \u0111\u1ec3 l\u1ea5y danh s\u00e1ch t\u00ean c\u00f3 trong b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">employees<\/span><\/i><span style=\"font-weight: 400;\"> nh\u01b0ng kh\u00f4ng c\u00f3 trong b\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">managers<\/span><\/i><span style=\"font-weight: 400;\">, ta s\u1eed d\u1ee5ng to\u00e1n t\u1eed <\/span><span style=\"font-weight: 400;\">MINUS<\/span><span style=\"font-weight: 400;\"> trong Oracle v\u1edbi c\u00fa ph\u00e1p sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">SELECT name FROM employees <\/span>\r\n\r\n<span style=\"font-weight: 400;\">MINUS <\/span>\r\n\r\n<span style=\"font-weight: 400;\">SELECT name FROM managers;<\/span><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Nhom_ham_tong_hop\"><\/span><b>Nh\u00f3m 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 (Aggregate functions) trong SQL \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c ph\u00e9p t\u00ednh tr\u00ean m\u1ed9t t\u1eadp h\u1ee3p d\u1eef li\u1ec7u v\u00e0 tr\u1ea3 v\u1ec1 m\u1ed9t gi\u00e1 tr\u1ecb duy nh\u1ea5t.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00e1c h\u00e0m n\u00e0y th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng c\u00f9ng v\u1edbi c\u00e2u l\u1ec7nh <\/span><span style=\"font-weight: 400;\">GROUP BY<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 nh\u00f3m c\u00e1c b\u1ea3n ghi l\u1ea1i v\u00e0 th\u1ef1c hi\u1ec7n c\u00e1c ph\u00e9p t\u00ednh tr\u00ean m\u1ed7i nh\u00f3m \u0111\u00f3. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 m\u1ed9t s\u1ed1 h\u00e0m t\u1ed5ng h\u1ee3p ph\u1ed5 bi\u1ebfn trong SQL:<\/span><\/p>\n<h3><strong>SUM()<\/strong><\/h3>\n<p>D\u00f9ng \u0111\u1ec3 t\u00ednh t\u1ed5ng c\u1ee7a m\u1ed9t t\u1eadp c\u00e1c gi\u00e1 tr\u1ecb s\u1ed1.<\/p>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 t\u00ednh t\u1ed5ng l\u01b0\u01a1ng c\u1ee7a t\u1ea5t c\u1ea3 nh\u00e2n vi\u00ean trong b\u1ea3ng employees, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/p>\n<pre>SELECT SUM(salary) AS total_salary\r\nFROM employees;<\/pre>\n<h3><strong>MIN()<\/strong><\/h3>\n<p>Tr\u1ea3 v\u1ec1 gi\u00e1 tr\u1ecb nh\u1ecf nh\u1ea5t trong m\u1ed9t t\u1eadp c\u00e1c gi\u00e1 tr\u1ecb.<\/p>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y m\u1ee9c l\u01b0\u01a1ng nh\u1ecf nh\u1ea5t trong b\u1ea3ng employees, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/p>\n<pre>SELECT MIN(salary) AS lowest_salary\r\nFROM employees;<\/pre>\n<h3><strong>MAX()<\/strong><\/h3>\n<p>Tr\u1ea3 v\u1ec1 gi\u00e1 tr\u1ecb l\u1edbn nh\u1ea5t trong m\u1ed9t t\u1eadp c\u00e1c gi\u00e1 tr\u1ecb.<\/p>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 l\u1ea5y m\u1ee9c l\u01b0\u01a1ng l\u1edbn nh\u1ea5t trong b\u1ea3ng employees, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/p>\n<pre>SELECT MAX(salary) AS highest_salary\r\nFROM employees;<\/pre>\n<h3><strong>COUNT()<\/strong><\/h3>\n<p>\u0110\u1ebfm s\u1ed1 l\u01b0\u1ee3ng b\u1ea3n ghi ho\u1eb7c gi\u00e1 tr\u1ecb kh\u00f4ng null.<\/p>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 \u0111\u1ebfm s\u1ed1 l\u01b0\u1ee3ng nh\u00e2n vi\u00ean c\u00f3 m\u1ee9c l\u01b0\u01a1ng l\u1edbn h\u01a1n 30000, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/p>\n<pre>SELECT COUNT(*) AS employees_above_30k\r\nFROM employees\r\nWHERE salary &gt; 30000;<\/pre>\n<h3><strong>AVG()<\/strong><\/h3>\n<p>T\u00ednh gi\u00e1 tr\u1ecb trung b\u00ecnh c\u1ee7a m\u1ed9t t\u1eadp c\u00e1c gi\u00e1 tr\u1ecb s\u1ed1.<\/p>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 t\u00ednh trung b\u00ecnh m\u1ee9c l\u01b0\u01a1ng c\u1ee7a nh\u00e2n vi\u00ean trong b\u1ea3ng employees, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/p>\n<pre>SELECT AVG(salary) AS average_salary\r\nFROM employees;<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Cac_rang_buoc_toan_ven\"><\/span><b>C\u00e1c r\u00e0ng bu\u1ed9c to\u00e0n v\u1eb9n <\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">C\u00e1c r\u00e0ng bu\u1ed9c to\u00e0n v\u1eb9n \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh lo\u1ea1i d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c ph\u00e9p nh\u1eadp v\u00e0o b\u1ea3ng, \u0111\u1ea3m b\u1ea3o r\u1eb1ng d\u1eef li\u1ec7u trong c\u01a1 s\u1edf d\u1eef li\u1ec7u lu\u00f4n ch\u00ednh x\u00e1c v\u00e0 h\u1ee3p l\u1ec7. Ta c\u00f3 c\u00e1c lo\u1ea1i r\u00e0ng bu\u1ed9c nh\u01b0 sau:<\/span><\/p>\n<h3><strong>R\u00e0ng bu\u1ed9c DEFAULT<\/strong><\/h3>\n<p>D\u00f9ng \u0111\u1ec3 cung c\u1ea5p gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh cho c\u1ed9t n\u1ebfu kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb n\u00e0o \u0111\u01b0\u1ee3c ch\u1ec9 \u0111\u1ecbnh khi nh\u1eadp d\u1eef li\u1ec7u.<\/p>\n<pre>column_name data_type DEFAULT default_value<\/pre>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 ch\u1ec9 \u0111\u1ecbnh gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh c\u1ee7a c\u1ed9t salary l\u00e0 30000 n\u1ebfu kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb n\u00e0o \u0111\u01b0\u1ee3c nh\u1eadp cho c\u1ed9t n\u00e0y khi ch\u00e8n 1 b\u1ea3n ghi v\u00e0o b\u1ea3ng employee, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/p>\n<pre>CREATE TABLE employees (\r\nsalary INT DEFAULT 30000\r\n);<\/pre>\n<h3><strong>R\u00e0ng bu\u1ed9c NOT NULL<\/strong><\/h3>\n<p>D\u00f9ng \u0111\u1ec3 c\u1ea5m gi\u00e1 tr\u1ecb NULL trong c\u1ed9t, \u0111\u1ea3m b\u1ea3o r\u1eb1ng m\u1ecdi b\u1ea3n ghi \u0111\u1ec1u c\u00f3 gi\u00e1 tr\u1ecb cho c\u1ed9t \u0111\u00f3.<\/p>\n<pre>column_name data_type NOT NULL<\/pre>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 th\u00eam r\u00e0ng bu\u1ed9c c\u1ed9t Name trong b\u1ea3ng Employees ph\u1ea3i c\u00f3 gi\u00e1 tr\u1ecb kh\u00e1c NULL, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/p>\n<pre>CREATE TABLE employees (\r\nname VARCHAR(50) NOT NULL\r\n);<\/pre>\n<h3><strong>R\u00e0ng bu\u1ed9c UNIQUE<\/strong><\/h3>\n<p>D\u00f9ng \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o r\u1eb1ng t\u1ea5t c\u1ea3 c\u00e1c gi\u00e1 tr\u1ecb trong c\u1ed9t l\u00e0 duy nh\u1ea5t v\u00e0 kh\u00f4ng tr\u00f9ng l\u1eb7p.<\/p>\n<pre>column_name data_type UNIQUE<\/pre>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 c\u1ed9t email trong b\u1ea3ng employees kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb tr\u00f9ng l\u1eb7p, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/p>\n<pre>CREATE TABLE employees (\r\nemail VARCHAR(100) UNIQUE\r\n);<\/pre>\n<h3><strong>R\u00e0ng bu\u1ed9c PRIMARY KEY<\/strong><\/h3>\n<p>L\u00e0 r\u00e0ng bu\u1ed9c d\u00f9ng \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh duy nh\u1ea5t m\u1ed7i h\u00e0ng (b\u1ea3n ghi) trong b\u1ea3ng. C\u1ed9t ho\u1eb7c nh\u00f3m c\u1ed9t \u0111\u01b0\u1ee3c \u0111\u1eb7t l\u00e0m kh\u00f3a ch\u00ednh s\u1ebd kh\u00f4ng cho ph\u00e9p tr\u00f9ng l\u1eb7p v\u00e0 kh\u00f4ng ch\u1ee9a gi\u00e1 tr\u1ecb NULL.<\/p>\n<p>Hay n\u00f3i c\u00e1ch kh\u00e1c th\u00ec \u0111\u00e2y l\u00e0 s\u1ef1 k\u1ebft h\u1ee3p c\u1ee7a r\u00e0ng bu\u1ed9c UNIQUE v\u00e0 NOT NULL. M\u1ed7i b\u1ea3ng ch\u1ec9 \u0111\u01b0\u1ee3c ph\u00e9p c\u00f3 1 kh\u00f3a ch\u00ednh.<\/p>\n<pre>column_name data_type PRIMARY KEY<\/pre>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 ch\u1ecdn c\u1ed9t employee_id trong b\u1ea3ng employees l\u00e0 kh\u00f3a ch\u00ednh, \u0111\u1ea3m b\u1ea3o r\u1eb1ng m\u1ed7i gi\u00e1 tr\u1ecb l\u00e0 duy nh\u1ea5t v\u00e0 kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb NULL, ta d\u00f9ng c\u00fa ph\u00e1p sau:<\/p>\n<pre>CREATE TABLE employees (\r\nemployee_id INT PRIMARY KEY\r\n);<\/pre>\n<h3><strong>R\u00e0ng bu\u1ed9c FOREIGN KEY<\/strong><\/h3>\n<p>R\u00e0ng bu\u1ed9c n\u00e0y d\u00f9ng \u0111\u1ec3 k\u1ebft n\u1ed1i hai b\u1ea3ng l\u1ea1i v\u1edbi nhau. C\u1ed9t \u0111\u01b0\u1ee3c khai b\u00e1o l\u00e0 kh\u00f3a ngo\u1ea1i trong b\u1ea3ng n\u00e0y s\u1ebd li\u00ean k\u1ebft v\u1edbi kh\u00f3a ch\u00ednh c\u1ee7a m\u1ed9t b\u1ea3ng kh\u00e1c. N\u00f3 \u0111\u1ea3m b\u1ea3o r\u1eb1ng m\u1ed9t c\u1ed9t (ho\u1eb7c m\u1ed9t nh\u00f3m c\u1ed9t) trong b\u1ea3ng n\u00e0y tham chi\u1ebfu \u0111\u1ebfn m\u1ed9t c\u1ed9t ho\u1eb7c nh\u00f3m c\u1ed9t trong b\u1ea3ng kh\u00e1c. Kh\u00f3a ngo\u1ea1i gi\u00fap duy tr\u00ec t\u00ednh to\u00e0n v\u1eb9n tham chi\u1ebfu gi\u1eefa c\u00e1c b\u1ea3ng.<\/p>\n<pre>FOREIGN KEY (column_name) REFERENCES other_table(column_name)<\/pre>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 t\u1ea1o b\u1ea3ng managers v\u1edbi c\u1ed9t manv l\u00e0 kh\u00f3a ngo\u1ea1i, li\u00ean k\u1ebft v\u1edbi c\u1ed9t manv trong b\u1ea3ng employees, ta c\u00f3 c\u00fa ph\u00e1p nh\u01b0 sau:<\/p>\n<pre>CREATE TABLE managers (\r\nmanv INT,\r\nname VARCHAR(50), FOREIGN KEY (manv) REFERENCES employees(manv)\r\n);<\/pre>\n<h3><strong>R\u00e0ng bu\u1ed9c CHECK<\/strong><\/h3>\n<p>R\u00e0ng bu\u1ed9c n\u00e0y d\u00f9ng \u0111\u1ec3 quy \u0111\u1ecbnh r\u1eb1ng gi\u00e1 tr\u1ecb trong m\u1ed9t c\u1ed9t ph\u1ea3i th\u1ecfa m\u00e3n m\u1ed9t \u0111i\u1ec1u ki\u1ec7n c\u1ee5 th\u1ec3, gi\u00fap \u0111\u1ea3m b\u1ea3o d\u1eef li\u1ec7u nh\u1eadp v\u00e0o h\u1ee3p l\u1ec7.<\/p>\n<pre>column_name data_type CHECK (condition)<\/pre>\n<p>V\u00ed d\u1ee5 \u0111\u1ec3 c\u00e1c gi\u00e1 tr\u1ecb c\u1ee7a c\u1ed9t salary trong b\u1ea3ng employees ch\u1ec9 ch\u1ea5p nh\u1eadn gi\u00e1 tr\u1ecb l\u1edbn h\u01a1n ho\u1eb7c b\u1eb1ng 20000 v\u00e0 c\u00e1c gi\u00e1 tr\u1ecb nh\u1ecf h\u01a1n s\u1ebd b\u1ecb t\u1eeb ch\u1ed1i, ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p:<\/p>\n<pre>CREATE TABLE employees (salary INT CHECK (salary &gt;= 20000) );<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_thuong_gap_ve_cau_lenh_SQL\"><\/span><b>C\u00e1c c\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 c\u00e2u l\u1ec7nh SQL<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>T\u1ea1i sao ph\u1ea3i s\u1eed d\u1ee5ng r\u00e0ng bu\u1ed9c to\u00e0n v\u1eb9n?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">R\u00e0ng bu\u1ed9c to\u00e0n v\u1eb9n gi\u00fap \u0111\u1ea3m b\u1ea3o t\u00ednh ch\u00ednh x\u00e1c v\u00e0 \u0111\u1ed9 tin c\u1eady c\u1ee7a d\u1eef li\u1ec7u trong c\u01a1 s\u1edf d\u1eef li\u1ec7u. Ngo\u00e0i ra r\u00e0ng bu\u1ed9c to\u00e0n v\u1eb9n c\u00f2n ng\u0103n ch\u1eb7n vi\u1ec7c nh\u1eadp c\u00e1c d\u1eef li\u1ec7u kh\u00f4ng h\u1ee3p l\u1ec7 ho\u1eb7c sai l\u1ec7ch, duy tr\u00ec t\u00ednh to\u00e0n v\u1eb9n c\u1ee7a d\u1eef li\u1ec7u.<\/span><\/p>\n<h3><b>S\u1ef1 kh\u00e1c bi\u1ec7t gi\u1eefa DELETE v\u00e0 TRUNCATE l\u00e0 g\u00ec?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">DELETE d\u00f9ng \u0111\u1ec3 x\u00f3a d\u1eef li\u1ec7u trong b\u1ea3ng, c\u00f3 th\u1ec3 k\u00e8m theo \u0111i\u1ec1u ki\u1ec7n WHERE. Khi s\u1eed d\u1ee5ng DELETE, h\u1ec7 th\u1ed1ng s\u1ebd t\u1ea1o ra m\u1ed9t l\u1ec7nh th\u1ef1c thi cho m\u1ed7i b\u1ea3n ghi th\u1ecfa m\u00e3n \u0111i\u1ec1u ki\u1ec7n x\u00f3a v\u00e0 c\u00e1c thao t\u00e1c n\u00e0y c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c ho\u00e0n t\u00e1c.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ng\u01b0\u1ee3c l\u1ea1i, TRUNCATE d\u00f9ng \u0111\u1ec3 x\u00f3a to\u00e0n b\u1ed9 d\u1eef li\u1ec7u trong b\u1ea3ng m\u00e0 kh\u00f4ng th\u1ec3 ho\u00e0n t\u00e1c v\u00e0 ch\u1ec9 ch\u1ea1y m\u1ed9t l\u1ea7n duy nh\u1ea5t, b\u1ea5t k\u1ec3 s\u1ed1 l\u01b0\u1ee3ng b\u1ea3n ghi trong b\u1ea3ng.<\/span><\/p>\n<h3><b>S\u1ef1 kh\u00e1c nhau gi\u1eefa DDL v\u00e0 DML l\u00e0 g\u00ec?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">DDL (Data Definition Language) g\u1ed3m c\u00e1c l\u1ec7nh nh\u01b0 <\/span><span style=\"font-weight: 400;\">CREATE<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">ALTER<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">DROP<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 \u0111\u1ecbnh ngh\u0129a v\u00e0 thay \u0111\u1ed5i c\u1ea5u tr\u00fac c\u1ee7a c\u01a1 s\u1edf d\u1eef li\u1ec7u v\u00e0 c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng b\u00ean trong n\u00f3.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00f2n DML (Data Manipulation Language) g\u1ed3m c\u00e1c l\u1ec7nh nh\u01b0 <\/span><span style=\"font-weight: 400;\">INSERT<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">UPDATE<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">DELETE<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 thao t\u00e1c d\u1eef li\u1ec7u b\u00ean trong b\u1ea3ng.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Tong_ket_Cau_lenh_SQL\"><\/span><b>T\u1ed5ng k\u1ebft C\u00e2u l\u1ec7nh SQL<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">C\u00e2u l\u1ec7nh SQL l\u00e0 m\u1ed9t ph\u1ea7n kh\u00f4ng th\u1ec3 thi\u1ebfu khi l\u00e0m vi\u1ec7c v\u1edbi c\u01a1 s\u1edf d\u1eef li\u1ec7u. Hi\u1ec3u r\u00f5 c\u00e1ch s\u1eed d\u1ee5ng c\u00e1c c\u00e2u l\u1ec7nh n\u00e0y gi\u00fap b\u1ea1n d\u1ec5 d\u00e0ng truy v\u1ea5n, x\u1eed l\u00fd v\u00e0 t\u1ed1i \u01b0u h\u00f3a d\u1eef li\u1ec7u. D\u00f9 b\u1ea1n l\u00e0 ng\u01b0\u1eddi m\u1edbi b\u1eaft \u0111\u1ea7u hay \u0111\u00e3 c\u00f3 kinh nghi\u1ec7m, vi\u1ec7c n\u1eafm v\u1eefng c\u00e1c c\u00e2u l\u1ec7nh SQL l\u00e0 b\u01b0\u1edbc \u0111\u1ea7u ti\u00ean \u0111\u1ec3 th\u00e0nh c\u00f4ng trong c\u00f4ng 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 v\u00e0 s\u1eed d\u1ee5ng \u0111\u01b0\u1ee3c c\u00e1c c\u00e2u l\u1ec7nh SQL th\u00f4ng d\u1ee5ng.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQL (Structured Query Language) l\u00e0 ng\u00f4n ng\u1eef chu\u1ea9n \u0111\u1ec3 qu\u1ea3n l\u00fd v\u00e0 thao t\u00e1c c\u01a1 s\u1edf d\u1eef li\u1ec7u quan h\u1ec7. T\u1eeb vi\u1ec7c t\u1ea1o b\u1ea3ng, th\u00eam d\u1eef li\u1ec7u \u0111\u1ebfn vi\u1ec7c truy xu\u1ea5t v\u00e0 c\u1eadp nh\u1eadt th\u00f4ng tin, SQL \u0111\u00f3ng vai tr\u00f2 quan tr\u1ecdng trong vi\u1ec7c t\u1ed5 ch\u1ee9c v\u00e0 khai th\u00e1c d\u1eef li\u1ec7u hi\u1ec7u qu\u1ea3. \u0110\u1ed1i v\u1edbi [&hellip;]<\/p>\n","protected":false},"author":209,"featured_media":79777,"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-79751","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.8) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>C\u00e2u l\u1ec7nh SQL: T\u1ed5ng h\u1ee3p c\u00e1c c\u00e2u l\u1ec7nh, to\u00e1n t\u1eed v\u00e0 r\u00e0ng bu\u1ed9c SQL - ITviec Blog<\/title>\n<meta name=\"description\" content=\"Kh\u00e1m ph\u00e1 c\u00e1c c\u00e2u l\u1ec7nh SQL c\u01a1 b\u1ea3n nh\u01b0ng th\u00f4ng d\u1ee5ng, gi\u00fap b\u1ea1n truy v\u1ea5n, x\u1eed l\u00fd v\u00e0 t\u1ed1i \u01b0u h\u00f3a d\u1eef li\u1ec7u m\u1ed9t c\u00e1ch hi\u1ec7u qu\u1ea3 cho m\u1ecdi d\u1ef1 \u00e1n.\" \/>\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\/tong-hop-cau-lenh-sql\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C\u00e2u l\u1ec7nh SQL: T\u1ed5ng h\u1ee3p c\u00e1c c\u00e2u l\u1ec7nh, to\u00e1n t\u1eed v\u00e0 r\u00e0ng bu\u1ed9c SQL\" \/>\n<meta property=\"og:description\" content=\"SQL (Structured Query Language) l\u00e0 ng\u00f4n ng\u1eef chu\u1ea9n \u0111\u1ec3 qu\u1ea3n l\u00fd v\u00e0 thao t\u00e1c c\u01a1 s\u1edf d\u1eef li\u1ec7u quan h\u1ec7. T\u1eeb vi\u1ec7c t\u1ea1o b\u1ea3ng, th\u00eam d\u1eef li\u1ec7u \u0111\u1ebfn vi\u1ec7c truy xu\u1ea5t v\u00e0 c\u1eadp\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/tong-hop-cau-lenh-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-10-02T01:56:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/10\/cau-lenh-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=\"24 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"C\u00e2u l\u1ec7nh SQL: T\u1ed5ng h\u1ee3p c\u00e1c c\u00e2u l\u1ec7nh, to\u00e1n t\u1eed v\u00e0 r\u00e0ng bu\u1ed9c SQL - ITviec Blog","description":"Kh\u00e1m ph\u00e1 c\u00e1c c\u00e2u l\u1ec7nh SQL c\u01a1 b\u1ea3n nh\u01b0ng th\u00f4ng d\u1ee5ng, gi\u00fap b\u1ea1n truy v\u1ea5n, x\u1eed l\u00fd v\u00e0 t\u1ed1i \u01b0u h\u00f3a d\u1eef li\u1ec7u m\u1ed9t c\u00e1ch hi\u1ec7u qu\u1ea3 cho m\u1ecdi d\u1ef1 \u00e1n.","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\/tong-hop-cau-lenh-sql\/","og_locale":"vi_VN","og_type":"article","og_title":"C\u00e2u l\u1ec7nh SQL: T\u1ed5ng h\u1ee3p c\u00e1c c\u00e2u l\u1ec7nh, to\u00e1n t\u1eed v\u00e0 r\u00e0ng bu\u1ed9c SQL","og_description":"SQL (Structured Query Language) l\u00e0 ng\u00f4n ng\u1eef chu\u1ea9n \u0111\u1ec3 qu\u1ea3n l\u00fd v\u00e0 thao t\u00e1c c\u01a1 s\u1edf d\u1eef li\u1ec7u quan h\u1ec7. T\u1eeb vi\u1ec7c t\u1ea1o b\u1ea3ng, th\u00eam d\u1eef li\u1ec7u \u0111\u1ebfn vi\u1ec7c truy xu\u1ea5t v\u00e0 c\u1eadp","og_url":"https:\/\/itviec.com\/blog\/tong-hop-cau-lenh-sql\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2024-10-02T01:56:29+00:00","og_image":[{"width":1500,"height":790,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/10\/cau-lenh-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":"24 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/tong-hop-cau-lenh-sql\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/tong-hop-cau-lenh-sql\/"},"author":{"name":"M\u1ef9 Duy\u00ean","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/73733c0725c7e39e696a896bd1abe2d7"},"headline":"C\u00e2u l\u1ec7nh SQL: T\u1ed5ng h\u1ee3p c\u00e1c c\u00e2u l\u1ec7nh, to\u00e1n t\u1eed v\u00e0 r\u00e0ng bu\u1ed9c SQL","datePublished":"2024-10-02T01:56:29+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/tong-hop-cau-lenh-sql\/"},"wordCount":6626,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/tong-hop-cau-lenh-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/10\/cau-lenh-SQL-vippro.jpg","articleSection":["Chuy\u00ean m\u00f4n IT"],"inLanguage":"vi"},{"@type":"WebPage","@id":"https:\/\/itviec.com\/blog\/tong-hop-cau-lenh-sql\/","url":"https:\/\/itviec.com\/blog\/tong-hop-cau-lenh-sql\/","name":"C\u00e2u l\u1ec7nh SQL: T\u1ed5ng h\u1ee3p c\u00e1c c\u00e2u l\u1ec7nh, to\u00e1n t\u1eed v\u00e0 r\u00e0ng bu\u1ed9c SQL - ITviec Blog","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itviec.com\/blog\/tong-hop-cau-lenh-sql\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/tong-hop-cau-lenh-sql\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/10\/cau-lenh-SQL-vippro.jpg","datePublished":"2024-10-02T01:56:29+00:00","description":"Kh\u00e1m ph\u00e1 c\u00e1c c\u00e2u l\u1ec7nh SQL c\u01a1 b\u1ea3n nh\u01b0ng th\u00f4ng d\u1ee5ng, gi\u00fap b\u1ea1n truy v\u1ea5n, x\u1eed l\u00fd v\u00e0 t\u1ed1i \u01b0u h\u00f3a d\u1eef li\u1ec7u m\u1ed9t c\u00e1ch hi\u1ec7u qu\u1ea3 cho m\u1ecdi d\u1ef1 \u00e1n.","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/tong-hop-cau-lenh-sql\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/tong-hop-cau-lenh-sql\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/tong-hop-cau-lenh-sql\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/10\/cau-lenh-SQL-vippro.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/10\/cau-lenh-SQL-vippro.jpg","width":1500,"height":790,"caption":"ca\u0302u le\u0323\u0302nh SQL - itviec blog"},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/tong-hop-cau-lenh-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":"C\u00e2u l\u1ec7nh SQL: T\u1ed5ng h\u1ee3p c\u00e1c c\u00e2u l\u1ec7nh, to\u00e1n t\u1eed v\u00e0 r\u00e0ng bu\u1ed9c SQL"}]},{"@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\/79751","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=79751"}],"version-history":[{"count":0,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/79751\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/79777"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=79751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=79751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=79751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}