<!DOCTYPE html>
    <html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
    <head>
<title>Mệnh đề HAVING trong SQL Server</title>
<meta name="description" content="Mệnh đề HAVING trong SQL Server - Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;www.nguoicodonvn2008.info&#x002F;vi&#x002F;news&#x002F;savefile&#x002F;kien-thuc-may-tinh&#x002F;menh-de-having-trong-sql-server-9670.html">
<meta name="author" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :.">
<meta name="copyright" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :. [admin@nguoicodonvn2008.info]">
<meta name="robots" content="index, archive, follow, noodp">
<meta name="googlebot" content="index,archive,follow,noodp">
<meta name="msnbot" content="all,index,follow">
<meta name="generator" content="NukeViet v4.5">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="Mệnh đề HAVING trong SQL Server">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;www.nguoicodonvn2008.info&#x002F;vi&#x002F;news&#x002F;savefile&#x002F;kien-thuc-may-tinh&#x002F;menh-de-having-trong-sql-server-9670.html">
<meta property="og:site_name" content=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.">
<meta property="og:url" content="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/menh-de-having-trong-sql-server-9670.html">
<link rel="shortcut icon" href="https://nguoicodonvn2008.info/favicon.ico">
<link rel="canonical" href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/menh-de-having-trong-sql-server-9670.html">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/karaoke-dual/" title="Tin Tức - Karaoke Dual" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/nhac-tre/" title="Tin Tức - Nhạc trẻ" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tru-tinh/" title="Tin Tức - Trữ tình" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/nuoc-ngoai/" title="Tin Tức - Nước ngoài" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/remix/" title="Tin Tức - Remix" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tam-su-tinh-yeu/" title="Tin Tức - Tâm sự tình yêu" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tho-suu-tam/" title="Tin Tức - Thơ sưu tầm" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/cuoc-song/" title="Tin Tức - Cuộc sống" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/phan-mem/" title="Tin Tức - Phần mềm" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/kien-thuc-may-tinh/" title="Tin Tức - Kiến thức máy tính" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/hoc-tap/" title="Tin Tức - Học tập" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/tai-lieu/" title="Tin Tức - Tài liệu" type="application/rss+xml">
<link rel="alternate" href="https://nguoicodonvn2008.info/vi/news/rss/de-thi/" title="Tin Tức - Đề thi" type="application/rss+xml">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/assets/css/font-awesome.min.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/style.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/style.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/news.css" type="text/css">
<link rel="preload" as="style" href="https://nguoicodonvn2008.info/themes/default/css/custom.css" type="text/css">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/jquery/jquery.min.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/language/vi.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/global.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/assets/js/site.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/news.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/main.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/custom.js" type="text/javascript">
<link rel="preload" as="script" href="https://nguoicodonvn2008.info/themes/default/js/bootstrap.min.js" type="text/javascript">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/assets/css/font-awesome.min.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/style.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://nguoicodonvn2008.info/themes/default/css/news.css">
<link rel="stylesheet" href="https://nguoicodonvn2008.info/themes/default/css/custom.css">
<style type="text/css">
	body{background: #fff;}
</style>
    </head>
    <body>
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">.&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.</h2>
		<p class="pull-right"><a title=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;." href="https://nguoicodonvn2008.info/">https://nguoicodonvn2008.info</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>Mệnh đề HAVING trong SQL Server</h1>
		<ul class="list-inline">
			<li>Thứ ba - 14/01/2025 22:38</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
		</div>
				<div class="imghome">
			<img alt="Mệnh đề HAVING trong SQL Server" src="https://st.quantrimang.com/photos/image/2018/03/31/ms-sql-server-alter-table-size-80x80-znd.jpg" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<p style="text-align: justify;"><strong>Having trong SQL là gì? Bạn đã biết cách dùng Group by having trong SQL chưa?&nbsp;</strong>Hãy cùng Quantrimang.com tìm hiểu nhé!</p>

<p style="text-align: justify;"><strong>Lệnh Having trong SQL Server&nbsp;</strong>cho phép người dùng lọc các kết quả truy vấn dựa trên những hàm tổng hợp và theo nhóm mà không thể đạt được bằng mệnh đề WHERE (được dùng để lọc từng hàng riêng lẻ).</p>

<p style="text-align: justify;">Nói đơn giản hơn, câu lệnh Having trong SQL Server được dùng để áp dụng một bộ lọc trên kết quả của GROUP BY dựa trên điều kiện cụ thể. Những điều kiện này là kiểu Boolean (ví dụ: dùng toán tử logic - AND, OR). Mệnh đề này được đưa vào SQL vì từ khóa WHERE lỗi khi dùng nó với các biểu thức tổng hợp. Having là mệnh đề được dùng rất phổ biến trong SQL. Tương tự WHERE, nó giúp áp dụng các điều kiện nhưng HAVING lại hoạt động theo nhóm. Nếu muốn lọc một nhóm, đây là lúc bạn cần tới hàm HAVING trong SQL Server.</p>

<p style="text-align: justify;">Một số điểm quan trọng:</p>

<ul>
	<li style="text-align: justify;">Mệnh đề HAVING được dùng để lọc dữ liệu theo các điều kiện được cung cấp.</li>
	<li style="text-align: justify;">Mệnh đề HAVING thường được dùng trong báo cáo chứa dữ liệu lớn.</li>
	<li style="text-align: justify;">Mệnh đề HAVING chỉ được dùng với mệnh đề SELECT.</li>
	<li style="text-align: justify;">Biểu thức này trong cú pháp chỉ có thể có hằng số.</li>
	<li style="text-align: justify;">Ở truy vấn, ORDER By được đặt sau mệnh đề HAVING nếu có.</li>
	<li style="text-align: justify;">Mệnh đề HAVING được triển khai trong chỉnh sửa cột.</li>
	<li style="text-align: justify;">Mệnh đề HAVING thường được dùng sau GROUP BY.</li>
</ul>

<h3 style="text-align: justify;">Cú pháp mệnh đề HAVING trong SQL Server</h3>

<pre id="pre0" style="text-align: justify;">
SELECT bieuthuc1, bieuthuc2, … bieuthuc_n,       ham_tong (bieuthuc)FROM bang&#91;WHERE dieukien&#93;GROUP BY bieuthuc1, bieuthuc2, … bieuthuc_nHAVING dieukien_having;</pre>

<h3 style="text-align: justify;">Tên biến hoặc giá trị biến</h3>

<p style="text-align: justify;"><strong>ham_tong</strong></p>

<p style="text-align: justify;">Có thể là các hàm như SUM, COUNT, MIN, MAX hoặc AVG.</p>

<p style="text-align: justify;"><strong>bieuthuc1, bieuthuc2, … bieuthuc_n</strong></p>

<p style="text-align: justify;">Biểu thức không nằm bên trong hàm tổng và phải nằm trong mệnh đề GROUP BY.</p>

<p style="text-align: justify;"><strong>WHERE dieukien</strong></p>

<p style="text-align: justify;">Tùy chọn. Các điều kiện mà bản ghi phải đáp ứng để được chọn.</p>

<p style="text-align: justify;"><strong>HAVING dieukien_having</strong></p>

<p style="text-align: justify;">Đây là điều kiện thêm chỉ áp dụng với kết quả tổng để giới hạn các nhóm của những hàng được trả về. Chỉ các nhóm mà điều kiện được đánh giá là TRUE mới nằm trong bộ kết quả.</p>

<p style="text-align: justify;"><strong>Ví dụ - dùng hàm SUM</strong></p>

<pre id="pre1" style="text-align: justify;">
SELECT bophan, SUM (soluong) AS “Tong so luong”FROM sanphamGROUP BY bophanHAVING SUM (soluong) &gt; 100;</pre>

<p style="text-align: justify;">Ví dụ về mệnh đề HAVING ở trên dùng hàm SUM để trả về tên của bộ phận và tổng số lượng (trong bộ phận liên quan). Mệnh đề HAVING sẽ lọc kết quả để chỉ các bộ phận có số lượng lớn hơn 100 được trả về.</p>

<p style="text-align: justify;"><strong>Ví dụ - dùng hàm COUNT</strong></p>

<pre id="pre2" style="text-align: justify;">
SELECT thanhpho, COUNT (*) AS “So nhanvien”FROM nhanvienWHERE bang = ‘California’GROUP BY thanhphoHAVING COUNT (*) &gt; 20;</pre>

<p style="text-align: justify;">Ví dụ này trả về thành phố và số nhân viên (ở thành phố đó) hiện đang ở bang California. Mệnh đề HAVING sẽ lọc để chỉ trả về các thành phố có nhiều hơn 20 nhân viên.</p>

<p style="text-align: justify;"><strong>Ví dụ - dùng hàm MIN</strong></p>

<pre id="pre3" style="text-align: justify;">
SELECT bophan, MIN (luong) AS “Luong thap nhat”FROM nhanvienGROUP BY bophanHAVING MIN (luong) &gt;= 50000;</pre>

<p style="text-align: justify;">Ở ví dụ này, kết quả trả về là tên của mỗi bộ phận và số lương tối thiểu trong từng bộ phận. Mệnh đề HAVING sẽ chỉ trả về các bộ phận có lương tối thiểu lớn hơn hoặc bằng $50.000.</p>

<p style="text-align: justify;"><strong>Ví dụ - dùng hàm MAX</strong></p>

<pre id="pre4" style="text-align: justify;">
SELECT ho, MAX (luong) AS “Luong cao nhat”FROM nhanvienGROUP BY bophanHAVING MAX (luong) &gt; 34000;</pre>

<p style="text-align: justify;">Trong ví dụ cuối cùng này, kết quả trả về là họ của nhân viên và số lương tối đa cho giá trị họ đó. Mệnh đề HAVING giới hạn chỉ trả về các giá trị họ có lương tối đa lớn hơn $34.000.</p>

<p style="text-align: justify;"><strong>Ví dụ - dùng hàm AVG()</strong></p>

<p style="text-align: justify;">Lệnh sau tìm các danh mục sản phẩm có giá niêm yết trung bình nằm trong khoảng từ 500 tới 1.000:</p>

<pre id="pre5">
<code>SELECT
    category_id,
    AVG (list_price) avg_list_price
FROM
    production.products
GROUP BY
    category_id
HAVING
    AVG (list_price) Giữa 500 và 1000;</code></pre>

<h2 style="text-align: justify;">Sự khác biệt giữa mệnh đề WHERE và mệnh đề HAVing trong SQL Server</h2>

<p style="text-align: justify;">Sự khác biệt giữa hai mệnh đề WHERE và Having trong database là câu hỏi thường xuất hiện nhiều nhất trong các buổi phỏng vấn.</p>

<p style="text-align: justify;">Bảng sau sẽ tổng kết sự khác biệt giữa hai mệnh đề này trong SQL Server. Tuy nhiên, điểm khác biệt chính ở đây là mệnh đề WHERE dùng điều kiện để lọc các bản ghi trước khi tạo nhóm bất kỳ, còn HAVING dùng điều kiện để lọc giá trị từ một nhóm.</p>

<table cellpadding="2" cellspacing="2">
	<tbody>
		<tr>
			<td>
			<p style="text-align: justify;"><strong>HAVING</strong></p>
			</td>
			<td>
			<p style="text-align: justify;"><strong>WHERE</strong></p>
			</td>
		</tr>
		<tr>
			<td>
			<p style="text-align: justify;">Mệnh đề HAVING được dùng trong hệ thống database để tìm nạp dữ liệu/giá trị từ các nhóm theo điều kiện được cung cấp.</p>
			</td>
			<td>
			<p style="text-align: justify;">Mệnh đề WHERE được dùng trong hệ thống database để tìm nạp dữ liệu/giá trị từ các bảng theo điều kiện được cung cấp.</p>
			</td>
		</tr>
		<tr>
			<td>
			<p style="text-align: justify;">Mệnh đề HAVING luôn được triển khai cùng với lệnh GROUP BY.</p>
			</td>
			<td>
			<p style="text-align: justify;">Mệnh đề WHERE có thể chạy mà không cần GROUP BY.</p>
			</td>
		</tr>
		<tr>
			<td>
			<p style="text-align: justify;">Mệnh đề HAVING có thể bao gồm các mệnh đề tổng hợp SQL trong một truy vấn hoặc câu lệnh.</p>
			</td>
			<td>
			<p style="text-align: justify;">Không thể dùng hàm tổng hợp trong SQL Server với mệnh đề WHERE trong các câu lệnh.</p>
			</td>
		</tr>
		<tr>
			<td>
			<p style="text-align: justify;">Chỉ có thể dùng lệnh SELECT với mệnh đề HAVING để lọc các bản ghi.</p>
			</td>
			<td>
			<p style="text-align: justify;">Dễ dàng dùng mệnh đề WHERE với UPDATE, DELETE và SELECT.</p>
			</td>
		</tr>
		<tr>
			<td>
			<p style="text-align: justify;">Mệnh đề HAVING được dùng trong truy vấn SQL sau lệnh GROUP BY.</p>
			</td>
			<td>
			<p style="text-align: justify;">Mệnh đề WHERE luôn được dùng trước GROUP BY trong truy vấn SQL.</p>
			</td>
		</tr>
		<tr>
			<td>
			<p style="text-align: justify;">Có thể triển khai mệnh đề SQL Server này trong các hoạt động cột.</p>
			</td>
			<td>
			<p style="text-align: justify;">Có thể triển khai mệnh đề SQL này trong các hoạt động ở hàng.</p>
			</td>
		</tr>
		<tr>
			<td>
			<p style="text-align: justify;">Nó là một bộ lọc sau.</p>
			</td>
			<td>
			<p style="text-align: justify;">Nó là một bộ lọc trước.</p>
			</td>
		</tr>
		<tr>
			<td>
			<p style="text-align: justify;">Nó được dùng để lọc nhóm.</p>
			</td>
			<td>
			<p style="text-align: justify;">Được dùng để lọc từng bản ghi trong bảng.</p>
			</td>
		</tr>
	</tbody>
</table>

<div style="text-align: justify;">&nbsp;</div>
		</div>
				<div id="author">
						<p>
				<strong>Nguồn tin:</strong>
				Quantrimang.com:
			</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/menh-de-having-trong-sql-server-9670.html" title="Mệnh đề HAVING trong SQL Server">https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/menh-de-having-trong-sql-server-9670.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; .&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.
		</div>
		<div id="contact">
			<a href="mailto:admin@nguoicodonvn2008.info">admin@nguoicodonvn2008.info</a>
		</div>
	</div>
</div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://nguoicodonvn2008.info/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
		</script>
		<div class="car-top">
  <span><img src="https://nguoicodonvn2008.info/themes/default/images/car.png" alt=""></span>
</div>
<script src="https://nguoicodonvn2008.info/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="news",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=-4,nv_my_abbr="EDT",nv_cookie_prefix="nv4c_e856T",nv_check_pass_mstime=1738000,nv_area_admin=0,nv_safemode=0,theme_responsive=0,nv_recaptcha_ver=2,nv_recaptcha_sitekey="",nv_recaptcha_type="image",XSSsanitize=1;</script>
<script src="https://nguoicodonvn2008.info/assets/js/language/vi.js"></script>
<script src="https://nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js"></script>
<script src="https://nguoicodonvn2008.info/assets/js/global.js"></script>
<script src="https://nguoicodonvn2008.info/assets/js/site.js"></script>
<script src="https://nguoicodonvn2008.info/themes/default/js/news.js"></script>
<script src="https://nguoicodonvn2008.info/themes/default/js/main.js"></script>
<script src="https://nguoicodonvn2008.info/themes/default/js/custom.js"></script>
<script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "Organization",
            "url": "https://nguoicodonvn2008.info",
            "logo": "https://nguoicodonvn2008.info/uploads/angel.gif"
        }
        </script>
<script src="https://nguoicodonvn2008.info/themes/default/js/bootstrap.min.js"></script>
<script type="text/javascript">
var $scrolltop = $('.car-top');
$scrolltop.on('click', function () {
    $('html,body').animate({
        scrollTop: 0
    }, 800);
    $(this).addClass("car-run");
    setTimeout(function(){ $scrolltop.removeClass('car-run');}, 1000);
    return false;
});
$(window).on('scroll', function ()
{ 
    if($(window).scrollTop() >= 200)
    {
        $scrolltop.addClass("show");
        $scrolltop.addClass("car-down");
    }
    else
    {
       $scrolltop.removeClass("show");
       setTimeout(function(){ $scrolltop.removeClass('car-down');}, 300);
    }
});
</script>
</body>
</html>