<!DOCTYPE html>
    <html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
    <head>
<title>Lệnh SELECT trong SQL Server</title>
<meta name="description" content="Lệnh SELECT 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;lenh-select-trong-sql-server-8617.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="Lệnh SELECT 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;lenh-select-trong-sql-server-8617.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/lenh-select-trong-sql-server-8617.html">
<link rel="shortcut icon" href="https://www.nguoicodonvn2008.info/favicon.ico">
<link rel="canonical" href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/lenh-select-trong-sql-server-8617.html">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/karaoke-dual/" title="Tin Tức - Karaoke Dual" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/nhac-tre/" title="Tin Tức - Nhạc trẻ" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tru-tinh/" title="Tin Tức - Trữ tình" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/nuoc-ngoai/" title="Tin Tức - Nước ngoài" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/remix/" title="Tin Tức - Remix" type="application/rss+xml">
<link rel="alternate" href="https://www.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://www.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://www.nguoicodonvn2008.info/vi/news/rss/cuoc-song/" title="Tin Tức - Cuộc sống" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/phan-mem/" title="Tin Tức - Phần mềm" type="application/rss+xml">
<link rel="alternate" href="https://www.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://www.nguoicodonvn2008.info/vi/news/rss/hoc-tap/" title="Tin Tức - Học tập" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tai-lieu/" title="Tin Tức - Tài liệu" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/de-thi/" title="Tin Tức - Đề thi" type="application/rss+xml">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/assets/css/font-awesome.min.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/style.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/style.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/news.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/custom.css" type="text/css">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/jquery/jquery.min.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/language/vi.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/global.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/site.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/news.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/main.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/custom.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/bootstrap.min.js" type="text/javascript">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/assets/css/font-awesome.min.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/style.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://www.nguoicodonvn2008.info/themes/default/css/news.css">
<link rel="stylesheet" href="https://www.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://www.nguoicodonvn2008.info/">https://www.nguoicodonvn2008.info</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>Lệnh SELECT trong SQL Server</h1>
		<ul class="list-inline">
			<li>Thứ năm - 04/07/2024 02:40</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="Lệnh SELECT trong SQL Server" src="https://st.quantrimang.com/photos/image/2023/09/04/sql-server-select-customers-table.png" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<p style="text-align: justify;"><strong>Câu lệnh SELECT trong SQL Server</strong>&nbsp;được dùng như thế nào? Hãy cùng Quantrimang.com tìm hiểu những điều bạn cần biết về&nbsp;<strong>select trong SQL Server&nbsp;</strong>nhé.</p>

<p style="text-align: justify;">Nếu đang học SQL Server, nhất định bạn nên nắm vững những hàm hay câu lệnh cơ bản, trong đó có SELECT.</p>

<h2 style="text-align: justify;">Hàm SELECT trong SQL Server</h2>

<p style="text-align: justify;">Bảng database là những đối tượng chứa tất cả dữ liệu trong cơ sở dữ liệu. Trong một bảng, dữ liệu được sắp xếp hợp lí theo định dạng hàng và cột, tương tự như một bảng tính.</p>

<p style="text-align: justify;">Mỗi hàng đại diện cho một bảng ghi duy nhất trong bảng, và mỗi cột đại diện cho một trường trong bảng ghi. Ví dụ, bảng customers chứa dữ liệu khách hàng như số định danh khách hàng, tên gọi, tên họ, điện thoại, email và thông tin địa chỉ như hình bên dưới:</p>

<p style="text-align: justify;"><img alt="Bảng khách hàng trong SQL Server" data-i="0" data-src="https://st.quantrimang.com/photos/image/2023/09/04/sql-server-select-customers-table.png" data-was-processed="true" height="171" src="https://st.quantrimang.com/photos/image/2023/09/04/sql-server-select-customers-table.png" width="650" /></p>

<p style="text-align: justify;">SQL Server dùng các lược đồ để nhóm hợp lý bảng và các đối tượng database khác. Ở cơ sở dữ liệu mẫu trên, chúng ta có hai schema: sales và production. Schema sales nhóm toàn bộ bảng liên quan tới doanh số bán hàng, còn schema production nhóm toàn bộ bảng liên quan tới sản xuất.</p>

<p style="text-align: justify;">Để truy vấn dữ liệu từ một bảng, bạn dùng các câu lệnh SELECT trong SQL Server. Dưới đây là mọi điều bạn cần biết về câu lệnh SELECT SQL Server.</p>

<p style="text-align: justify;"><strong>Những điều cần biết khi dùng select trong SQL Server:</strong></p>

<ul>
	<li style="text-align: justify;">Có sẵn số lượng lớn dữ liệu ở khắp mọi nơi.</li>
	<li style="text-align: justify;">Dữ liệu được lưu trữ theo thứ tự dưới dạng bảng.</li>
	<li style="text-align: justify;">Mỗi bảng bao gồm một hàng đại diện cho một bản ghi duy nhất và cột đại diện cho một trường.</li>
	<li style="text-align: justify;">Sơ đồ - Schemas được dùng để sắp xếp bảng theo thứ tự logic.</li>
	<li style="text-align: justify;">Để trích xuất dữ liệu cụ thể từ bảng, các truy vấn viết bằng SQL được sử dụng.</li>
	<li style="text-align: justify;">Để truy vấn dữ liệu, dùng lệnh select.</li>
</ul>

<div style="text-align: justify;">&nbsp;</div>

<h2 style="text-align: justify;">Cú pháp cơ bản của lệnh select trong SQL Server</h2>

<pre id="pre0" style="text-align: justify;">
select
select_list;
from
schema_name.table.name;</pre>

<p style="text-align: justify;">Để truy xuất tất cả cột từ bảng, select trong SQL Server được sử dụng.</p>

<pre id="pre1" style="text-align: justify;">
Syntax -
select*
from
table_name;</pre>

<p style="text-align: justify;"><strong>Những điểm cần ghi nhớ:</strong></p>

<ul>
	<li style="text-align: justify;">Trong cơ sở dữ liệu tại thời gian thực, lệnh select không được khuyến khích sử dụng vì nó truy xuất dữ liệu nhiều hơn yêu cầu của bạn.</li>
	<li style="text-align: justify;">Kết quả nó khiến ứng dụng chạy chậm.</li>
	<li style="text-align: justify;">Trong trường hợp, người dùng thêm nhiều cột hơn vào bảng, lệnh select truy xuất toàn bộ cột, bao gồm cả cột mới nên khiến ứng dụng bị treo.</li>
</ul>

<p style="text-align: justify;">Ở dạng đơn giản, cú pháp của lệnh SELECT như sau:</p>

<pre id="pre2" style="text-align: justify;">
SELECT “biểu thức”
 FROM “bảng”
 &#91;WHERE “điều kiện”&#93;;</pre>

<p style="text-align: justify;">Ở dạng đầy đủ, cú pháp của lệnh SELECT trong SQL Server sẽ là:</p>

<pre id="pre3" style="text-align: justify;">
SELECT &#91; ALL | DISTINCT &#93;
&#91; TOP (gia_tri_dau) &#91; PERCENT &#93; &#91; WITH TIES &#93; &#93;
“Biểu thức”
FROM “bảng”
&#91;WHERE “điều kiện”&#93;
&#91;GROUP BY “biểu thức”&#93;
&#91;HAVING “điều kiện”&#93;
&#91;ORDER BY “biểu thức” &#91; ASC | DESC &#93;&#93;;</pre>

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

<p style="text-align: justify;"><strong>ALL</strong>: Tùy chọn, trả lại tất cả các hàng phù hợp.</p>

<p style="text-align: justify;"><strong>DISTINCT</strong>: Tùy chọn. Loại bỏ tất cả các giá trị trùng lặp khỏi bộ kết quả.</p>

<p style="text-align: justify;"><strong>TOP</strong>&nbsp;<strong>(gia_tri_dau)</strong>: Tùy chọn. Nếu chỉ định cụ thể sẽ trả về những giá trị đầu trong bộ kết quả dựa trên&nbsp;<em>gia_tri_dau</em>&nbsp;đã chon. Ví dụ TOP(10) sẽ trả về 10 hàng đầu tiên trong bộ kết quả.</p>

<p style="text-align: justify;"><strong>PERCENT</strong>: Tùy chọn. Nếu chỉ định cụ thể thì các hàng đầu tiên dựa theo phần trăm trên bộ kết quả (chỉ định bằng&nbsp;<em>gia_tri_dau</em>). Ví dụ TOP(10) PERCENT sẽ trả về top 10% kết quả đầu tiên trong bộ kết quả.</p>

<p style="text-align: justify;"><strong>WITH TIES</strong>: Tùy chọn. Nếu chỉ định cụ thể thì các hàng cố định ở cuối trong kết quả có giới hạn sẽ được trả về. Điều này có thể khiến cho số hàng trả về nhiều hơn biến TOP cho phép.</p>

<p style="text-align: justify;"><strong>Biểu thức</strong>: Cột hay giá trị tính toán mà bạn muốn lấy về. Dùng&nbsp;<em>*</em>&nbsp;nếu muốn lấy tất cả các cột.</p>

<p style="text-align: justify;"><strong>Bảng</strong>: Bảng mà bạn muốn lấy kết quả về. Phải có ít nhất 1 bảng được liệt kê trong lệnh FROM.</p>

<p style="text-align: justify;"><strong>WHERE “điều kiện”</strong>: Tùy chọn. Điều kiện mà kết quả trả về phải đáp ứng được.</p>

<p style="text-align: justify;"><strong>GROUP BY “biểu thức”</strong>: Tùy chọn. Thu thập dữ liệu từ nhiều bản ghi và nhóm kết quả theo một hoặc nhiều cột.</p>

<div style="text-align: justify;">&nbsp;</div>

<p style="text-align: justify;"><strong>HAVING “điều kiện”</strong>: Tùy chọn. Dùng kết hợp với<strong>&nbsp;GROUP BY</strong>&nbsp;để giới hạn nhóm các hàng trả về khi điều kiện được đáp ứng là TRUE.</p>

<p style="text-align: justify;"><strong>ORDER BY “biểu thức”</strong>: Tùy chọn. Được dùng để lọc bộ kết quả.&nbsp;<strong>ASC</strong>&nbsp;sẽ lọc theo thứ tự tăng dần và&nbsp;<strong>DESC</strong>&nbsp;sẽ lọc theo thứ tự giảm dần.</p>

<h2 style="text-align: justify;">Ví dụ về SELECT trong SQL Server</h2>

<p style="text-align: justify;">Giả sử ta có bảng dữ liệu&nbsp;<strong>Quantrimang.com</strong>&nbsp;như sau:</p>

<pre id="pre4" style="text-align: justify;">
+-------------+--------------+-------------+-------+
|IDChuyenmuc  |Muccon        |Chuyenmuclon |Sobai  |
+-------------+--------------+-------------+-------+
|    1        |SQL Server    |Lap trinh    |101    |
|    2        |Facebook      |Mang xa hoi  |152    |
|    3        |Python        |Lap trinh    |111    |
|    4        |JavaScript    |Lap trinh    |122    |
|    5        |Chrome        |Web          |94     |
|    6        |Instagram     |Mang xa hoi  |165    |
+-------------+--------------+-------------+-------+
</pre>

<p style="text-align: justify;"><strong>Ví dụ dùng SELECT chọn tất cả các trường trong bảng</strong></p>

<pre id="pre5" style="text-align: justify;">
SELECT * FROM &#91;Quantrimang.com&#93;
  WHERE Sobai &gt; 111
  ORDER BY Sobai ASC;</pre>

<p style="text-align: justify;">Trong ví dụ này, * được dùng để cho biết sẽ chọn tất cả các trường có&nbsp;<em>Sobai</em>&nbsp;lớn hơn 111 trong bảng&nbsp;<strong>Quantrimang.com</strong>. Bộ kết quả được sắp xếp theo thứ tự tăng dần của&nbsp;<em>Sobai</em>.</p>

<pre id="pre6" style="text-align: justify;">
+-------------+--------------+-------------+-------+
|IDChuyenmuc  |Muccon        |Chuyenmuclon |Sobai  |
+-------------+--------------+-------------+-------+
|    4        |JavaScript    |Lap trinh    |122    |
|    2        |Facebook      |Mang xa hoi  |152    |
|    6        |Instagram     |Mang xa hoi  |165    |
+-------------+--------------+-------------+-------+
</pre>

<p style="text-align: justify;"><strong>Ví dụ dùng SELECR chọn một số trường trong bảng</strong></p>

<pre id="pre7" style="text-align: justify;">
SELECT IDChuyenmuc, Chuyenmuclon, Sobai
  FROM &#91;Quantrimang.com&#93;
  WHERE IDChuyenmuc &gt; 2
  AND Chuyenmuclon = &#039;Lap trinh&#039;
  Order By Sobai DESC;</pre>

<p style="text-align: justify;">Ở ví dụ nói trên, kết quả trả về sẽ chỉ gồm có&nbsp;<em>IDChuyenmuc, Chuyenmuclon, Sobai</em>&nbsp; với&nbsp;<em>IDChuyenmuc</em>&nbsp;lớn hơn 2 và&nbsp;<em>Chuyenmuclon</em>&nbsp;là &#039;Lap trinh&#039;. Kết quả trả về được sắp xếp theo&nbsp;<em>Sobai</em>&nbsp;giảm dần như dưới đây:</p>

<pre id="pre8" style="text-align: justify;">
+-------------+-------------+-------+
|IDChuyenmuc  |Chuyenmuclon |Sobai  |
+-------------+-------------+-------+
|    4        |Lap trinh    |122    |
|    3        |Lap trinh    |111    |
+-------------+-------------+-------+</pre>

<p style="text-align: justify;"><strong>Ví dụ dung SELECT chọn từ nhiều bảng</strong></p>

<p style="text-align: justify;">Để thực hiện ví dụ này, ta có thêm bảng&nbsp;<strong>Muc</strong>&nbsp;như sau:</p>

<pre id="pre9" style="text-align: justify;">
+----+-----------+-------------+
| ID |Ten muc    |Trang thai   |
+----+-----------+-------------+
| 1  |Lap trinh  |Hien         |
| 2  |Mang xa hoi|An           |
| 3  |Web        |An           |
+----+-----------+-------------+
</pre>

<p style="text-align: justify;">Giả sử ta cần lấy những&nbsp;<em>Muccon</em>&nbsp;có&nbsp;<em>Trangthai</em>&nbsp;là&nbsp;<em>Hien</em>, và những bản ghi phải thỏa mãn<em>&nbsp;Chuyenmuclon</em>&nbsp;của bảng&nbsp;<strong>Quantrimang.com</strong>&nbsp;và&nbsp;<em>Tenmuc</em>&nbsp;của bảng&nbsp;<strong>Muc</strong>&nbsp;phải trùng nhau và kết quả trả về sắp xếp theo thứ tự ASC của cột&nbsp;<em>Muccon</em>.</p>

<div style="text-align: justify;">&nbsp;</div>

<pre id="pre10" style="text-align: justify;">
SELECT &#91;Quantrimang.com&#93;.Muccon,&#91;Quantrimang.com&#93;.Chuyenmuclon,Muc.Trangthai
  FROM &#91;Quantrimang.com&#93;
  INNER JOIN Muc
  ON &#91;Quantrimang.com&#93;.Chuyenmuclon=Muc.Tenmuc AND Muc.Trangthai= &#039;Hien&#039;
  ORDER BY &#91;Quantrimang.com&#93;.Muccon ASC;</pre>

<p style="text-align: justify;">Kết quả trả về là bảng dữ liệu sau:</p>

<pre id="pre11" style="text-align: justify;">
+----------+------------+----------+
|Muccon    |Chuyenmuclon|Trangthai |
+----------+------------+----------+
|Chrome    |Web         |Hien      |
|JavaScript|Lap trinh   |Hien      |
|Python    |Lap trinh   |Hien      |
|SQL Server|Lap trinh   |Hien      |
+----------+------------+----------+
</pre>

<p style="text-align: justify;"><strong>Ví dụ dùng SELECT với từ khóa TOP</strong></p>

<pre id="pre12" style="text-align: justify;">
SELECT TOP(2) *
  FROM &#91;Quantrimang.com&#93;
  WHERE Chuyenmuclon=&#039;Lap trinh&#039;
  ORDER BY IDChuyenmuc ASC;</pre>

<p style="text-align: justify;">Ở ví dụ này, kết quả sẽ trả về 2 giá trị đầu tiên từ bảng&nbsp;<strong>Quantrimang.com</strong>&nbsp;với&nbsp;<em>Chuyemuclon</em>&nbsp;là &#039;Lap trinh&#039;. Dù còn các giá trị khác đáp ứng yêu cầu thì chúng cũng sẽ không được trả về.</p>

<pre id="pre13" style="text-align: justify;">
+-------------+--------------+-------------+-------+
|IDChuyenmuc  |Muccon        |Chuyenmuclon |Sobai  |
+-------------+--------------+-------------+-------+
|    1        |SQL Server    |Lap trinh    |101    |
|    3        |Python        |Lap trinh    |111    |
+-------------+--------------+-------------+-------+
</pre>

<p style="text-align: justify;"><strong>Ví dụ - Dùng từ khóa TOP PERCENT</strong></p>

<pre id="pre14" style="text-align: justify;">
SELECT TOP(10) PERCENT *
  FROM &#91;Quantrimang.com&#93;
  WHERE Chuyenmuclon=&#039;Lap trinh&#039;
  ORDER BY IDChuyenmuc ASC;</pre>

<p style="text-align: justify;">Kết quả trả về sẽ gồm 10% giá trị đầu tiên trong toàn bộ kết quả&nbsp;<em>Chuyenmuclon</em>&nbsp;là ‘Lap trinh’ trong bảng&nbsp;<strong>Quantrimang.com</strong>. 90% kết quả còn lại sẽ không được trả về.</p>

<pre id="pre15" style="text-align: justify;">
+-------------+--------------+-------------+-------+
|IDChuyenmuc  |Muccon        |Chuyenmuclon |Sobai  |
+-------------+--------------+-------------+-------+
|    1        |SQL Server    |Lap trinh    |101    |
+-------------+--------------+-------------+-------+
</pre>

<p style="text-align: justify;"><strong>Dùng DISTINCT với SELECT</strong></p>

<p style="text-align: justify;">Ví dụ sau sùng DISTINCT để ngăn truy xuất các tiêu đề trùng lặp:</p>

<pre id="pre16" style="text-align: justify;">
USE AdventureWorks2012;
GO
SELECT DISTINCT JobTitle
FROM HumanResources.Employee
ORDER BY JobTitle;
GO</pre>

<p style="text-align: justify;"><strong>Tạo bảng với SELECT INTO</strong></p>

<p style="text-align: justify;">Ví dụ đầu tiên tạo một bảng tạm thời mang tên #Bicycles trong tempdb.</p>

<pre id="pre17" style="text-align: justify;">
USE tempdb;
GO
IF OBJECT_ID (N&#039;#Bicycles&#039;,N&#039;U&#039;) IS NOT NULL
DROP TABLE #Bicycles;
GO
SELECT * 
INTO #Bicycles
FROM AdventureWorks2012.Production.Product
WHERE ProductNumber LIKE &#039;BK%&#039;;
GO</pre>

<p style="text-align: justify;">Ví dụ thứ hai tạo bảng cố định mang tên NewProducts.</p>

<pre id="pre18" style="text-align: justify;">
USE AdventureWorks2012;
GO
IF OBJECT_ID(&#039;dbo.NewProducts&#039;, &#039;U&#039;) IS NOT NULL
    DROP TABLE dbo.NewProducts;
GO
ALTER DATABASE AdventureWorks2012 SET RECOVERY BULK_LOGGED;
GO

SELECT * INTO dbo.NewProducts
FROM Production.Product
WHERE ListPrice &gt; $25 
AND ListPrice &lt; $100;
GO
ALTER DATABASE AdventureWorks2012 SET RECOVERY FULL;
GO</pre>

<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/lenh-select-trong-sql-server-8617.html" title="Lệnh SELECT trong SQL Server">https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/lenh-select-trong-sql-server-8617.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://www.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://www.nguoicodonvn2008.info/themes/default/images/car.png" alt=""></span>
</div>
<script src="https://www.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://www.nguoicodonvn2008.info/assets/js/language/vi.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/global.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/site.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/news.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/main.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/custom.js"></script>
<script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "Organization",
            "url": "https://www.nguoicodonvn2008.info",
            "logo": "https://www.nguoicodonvn2008.info/uploads/angel.gif"
        }
        </script>
<script src="https://www.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>