{"id":102,"date":"2018-04-11T22:18:34","date_gmt":"2018-04-11T15:18:34","guid":{"rendered":"http:\/\/www.sqlservertutorial.net\/?page_id=102"},"modified":"2024-09-24T20:12:45","modified_gmt":"2024-09-24T13:12:45","slug":"sql-server-select","status":"publish","type":"page","link":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/","title":{"rendered":"SQL Server SELECT"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: This tutorial introduces you to the basics of the SQL Server <code>SELECT<\/code> statement, focusing on how to retrieve data from a single table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='basic-sql-server-select-statement'>Basic SQL Server SELECT statement <a href=\"#basic-sql-server-select-statement\" class=\"anchor\" id=\"basic-sql-server-select-statement\" title=\"Anchor for Basic SQL Server SELECT statement\">#<\/a><\/h2>\n\n\n\n<p>In SQL Server, tables are objects that store all the data in a database. They organize data in a row-and-column format, similar to a spreadsheet. Each row represents a unique record in a table, and each column represents a field in that record. <\/p>\n\n\n\n<p>For example, the following\u00a0<code>customers<\/code> table contains customer data such as customer ID, first name, last name, phone, email, and address:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"789\" height=\"208\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-customers-table.png\" alt=\"Customers table\" class=\"wp-image-164\" srcset=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-customers-table.png 789w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-customers-table-300x79.png 300w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-customers-table-768x202.png 768w\" sizes=\"auto, (max-width: 789px) 100vw, 789px\" \/><\/figure>\n\n\n\n<p>SQL Server uses <em>schemas <\/em>to logically group tables and other database objects. For example, our <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-sample-database\/\">sample database<\/a> has two schemas: <code>sales<\/code> and <code>production<\/code>. <\/p>\n\n\n\n<p>The <code>sales<\/code> schema includes all the sales-related tables, while the <code>production<\/code> schema groups all the production-related tables.<\/p>\n\n\n\n<p>To retrieve data from a table, you use the <code>SELECT<\/code> statement with the following syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    select_list\n<span class=\"hljs-keyword\">FROM<\/span>\n    schema_name.table_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, specify a list of comma-separated columns from which you want to query data in&nbsp;the <code>SELECT<\/code> clause.<\/li>\n\n\n\n<li>Second, specify the table name and its schema in the <code>FROM<\/code> clause.<\/li>\n<\/ul>\n\n\n\n<p>When processing the <code>SELECT<\/code> statement, SQL Server first processes the <code>FROM<\/code> clause, followed by the <code>SELECT<\/code> clause, even though the <code>SELECT<\/code> clause appears before the <code>FROM<\/code> clause:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"227\" height=\"57\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-SELECT-clause-order-evaluation.png\" alt=\"SQL Server SELECT - clause order evaluation\" class=\"wp-image-1612\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='sql-server-select-statement-examples'>SQL Server SELECT statement examples <a href=\"#sql-server-select-statement-examples\" class=\"anchor\" id=\"sql-server-select-statement-examples\" title=\"Anchor for SQL Server SELECT statement examples\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s use the <code>customers<\/code> table in the <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-sample-database\/\">sample database<\/a> for the demonstration.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"181\" height=\"231\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/customers.png\" alt=\"\" class=\"wp-image-158\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='1-basic-sql-server-select-statement-example'>1) Basic SQL Server SELECT statement example <a href=\"#1-basic-sql-server-select-statement-example\" class=\"anchor\" id=\"1-basic-sql-server-select-statement-example\" title=\"Anchor for 1) Basic SQL Server SELECT statement example\">#<\/a><\/h3>\n\n\n\n<p>The following query uses a <code>SELECT<\/code> statement to retrieve the first and last names of all customers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    first_name,\n    last_name\n<span class=\"hljs-keyword\">FROM<\/span>\n    sales.customers;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here is the result:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"133\" height=\"235\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-some-columns.png\" alt=\"sql server select - some columns\" class=\"wp-image-170\"\/><\/figure>\n\n\n\n<p>The result of a query is often called a <strong>result set<\/strong>.<\/p>\n\n\n\n<p>The following statement uses the <code>SELECT<\/code> statement to retrieve the first name, last name, and email of all customers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    first_name,\n    last_name,\n    email\n<span class=\"hljs-keyword\">FROM<\/span>\n    sales.customers;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"298\" height=\"246\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-select-three-columns.png\" alt=\"sql server select - select three columns\" class=\"wp-image-169\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='2-using-the-sql-server-select-to-retrieve-all-columns-of-a-table'>2) Using the SQL Server SELECT to retrieve all columns of a table <a href=\"#2-using-the-sql-server-select-to-retrieve-all-columns-of-a-table\" class=\"anchor\" id=\"2-using-the-sql-server-select-to-retrieve-all-columns-of-a-table\" title=\"Anchor for 2) Using the SQL Server SELECT to retrieve all columns of a table\">#<\/a><\/h3>\n\n\n\n<p>To retrieve data from all table columns, you can specify all the columns in the <code>SELECT<\/code> list. Alternatively, you can also use&nbsp;<code>SELECT *<\/code>&nbsp;as a shorthand to select all columns:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> sales.customers;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"788\" height=\"246\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-select-all-columns.png\" alt=\"sql server select - select all columns\" class=\"wp-image-168\" srcset=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-select-all-columns.png 788w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-select-all-columns-300x94.png 300w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-select-all-columns-768x240.png 768w\" sizes=\"auto, (max-width: 788px) 100vw, 788px\" \/><\/figure>\n\n\n\n<p>Using the <code>SELECT *<\/code> is useful for examining the table that you are not familiar with and it is particularly helpful for ad-hoc queries.<\/p>\n\n\n\n<p>However, you should not use the <code>SELECT *<\/code> in production code for the following main reasons:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, using\u00a0<code>SELECT *<\/code> often retrieves more data than your application needs. This unnecessary data takes more time to transfer from the database server to the application, slowing down the application.<\/li>\n\n\n\n<li>Second, if new columns are added to the table, <code>SELECT *<\/code> will retrieve all columns, including the new ones that your application may not expect. This could potentially cause the application to behave unexpectedly.<\/li>\n<\/ul>\n\n\n\n<p>In the following section, we&#8217;ll briefly introduce the additional clauses of the SELECT statement:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-where\/\">WHERE<\/a> : filter rows in the result set.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-order-by\/\">ORDER BY<\/a>: sort rows in the result set by one or more columns.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-group-by\/\">GROUP BY<\/a>: group rows into groups.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-having\/\">HAVING<\/a>: filter groups.<\/li>\n<\/ul>\n\n\n\n<p class=\"note\">Please note that we&#8217;ll cover these clauses in greater detail in the upcoming tutorials.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='3-filtering-rows-using-the-where-clause'>3) Filtering rows using the WHERE clause <a href=\"#3-filtering-rows-using-the-where-clause\" class=\"anchor\" id=\"3-filtering-rows-using-the-where-clause\" title=\"Anchor for 3) Filtering rows using the WHERE clause\">#<\/a><\/h3>\n\n\n\n<p>To filter rows based on one or more conditions, you use a <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-where\/\">WHERE<\/a><\/code> clause. <\/p>\n\n\n\n<p>For example, the following <code>SELECT<\/code> statement uses a <code>WHERE<\/code> clause to find customers located in <code>California<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    *\n<span class=\"hljs-keyword\">FROM<\/span>\n    sales.customers\n<span class=\"hljs-keyword\">WHERE<\/span>\n    state = <span class=\"hljs-string\">'CA'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"794\" height=\"228\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-where-clause.png\" alt=\"sql server select - where clause\" class=\"wp-image-171\" srcset=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-where-clause.png 794w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-where-clause-300x86.png 300w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-where-clause-768x221.png 768w\" sizes=\"auto, (max-width: 794px) 100vw, 794px\" \/><\/figure>\n\n\n\n<p>If the <code>SELECT<\/code> statement includes both <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-where\/\">WHERE<\/a> and <code>FROM<\/code> clauses, SQL Server processes them in the following sequence: <code>FROM<\/code>, <code>WHERE<\/code>, and <code>SELECT<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"370\" height=\"59\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-SELECT-from-where-select.png\" alt=\"SQL Server SELECT - from where select\" class=\"wp-image-1613\" srcset=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-SELECT-from-where-select.png 370w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-SELECT-from-where-select-300x48.png 300w\" sizes=\"auto, (max-width: 370px) 100vw, 370px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='4-sorting-rows-using-the-order-by-clause'>4) Sorting rows using the ORDER BY clause <a href=\"#4-sorting-rows-using-the-order-by-clause\" class=\"anchor\" id=\"4-sorting-rows-using-the-order-by-clause\" title=\"Anchor for 4) Sorting rows using the ORDER BY clause\">#<\/a><\/h3>\n\n\n\n<p>To sort rows in a result set based, you use the <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-order-by\/\">ORDER BY<\/a><\/code> clause. For example, the following query uses the <code>ORDER BY<\/code> clause to sort customers by their first names in ascending order.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    *\n<span class=\"hljs-keyword\">FROM<\/span>\n    sales.customers\n<span class=\"hljs-keyword\">WHERE<\/span>\n    state = <span class=\"hljs-string\">'CA'<\/span>\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n    first_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"831\" height=\"245\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-order-by-clause.png\" alt=\"sql server select - order by clause\" class=\"wp-image-167\" srcset=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-order-by-clause.png 831w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-order-by-clause-300x88.png 300w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-order-by-clause-768x226.png 768w\" sizes=\"auto, (max-width: 831px) 100vw, 831px\" \/><\/figure>\n\n\n\n<p>When the <code>SELECT<\/code> statement includes the <code>FROM<\/code>, <code>WHERE<\/code>, and <code>ORDER BY<\/code> clause, SQL Server processes them in the following order: <code>FROM<\/code>, <code>WHERE<\/code>, <code>SELECT<\/code>, and <code>ORDER BY<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"507\" height=\"59\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-SELECT-from-where-select-order-by.png\" alt=\"SQL Server SELECT - from where select order by\" class=\"wp-image-1615\" srcset=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-SELECT-from-where-select-order-by.png 507w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-SELECT-from-where-select-order-by-300x35.png 300w\" sizes=\"auto, (max-width: 507px) 100vw, 507px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='5-grouping-rows-into-groups'>5) Grouping rows into groups <a href=\"#5-grouping-rows-into-groups\" class=\"anchor\" id=\"5-grouping-rows-into-groups\" title=\"Anchor for 5) Grouping rows into groups\">#<\/a><\/h3>\n\n\n\n<p>To group rows into groups, you use the <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-group-by\/\">GROUP BY<\/a><\/code> clause. <\/p>\n\n\n\n<p>For example, the following statement returns all the cities of customers located in California and the number of customers in each city.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    city,\n    <span class=\"hljs-keyword\">COUNT<\/span> (*)\n<span class=\"hljs-keyword\">FROM<\/span>\n    sales.customers\n<span class=\"hljs-keyword\">WHERE<\/span>\n    state = <span class=\"hljs-string\">'CA'<\/span>\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n    city\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n    city;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"190\" height=\"264\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-group-by-clause.png\" alt=\"sql server select - group by clause\" class=\"wp-image-165\"\/><\/figure>\n\n\n\n<p>In this case, SQL Server processes the clauses in the following order: <code>FROM<\/code>, <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-where\/\">WHERE<\/a><\/code>, <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-group-by\/\">GROUP BY<\/a><\/code>, <code>SELECT<\/code>, and <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-order-by\/\">ORDER BY<\/a><\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"642\" height=\"60\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-SELECT-from-where-group-by-select-order-by.png\" alt=\"\" class=\"wp-image-1616\" srcset=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-SELECT-from-where-group-by-select-order-by.png 642w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-SELECT-from-where-group-by-select-order-by-300x28.png 300w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='6-filtering-groups-using-the-having-clause'>6) Filtering groups using the HAVING clause <a href=\"#6-filtering-groups-using-the-having-clause\" class=\"anchor\" id=\"6-filtering-groups-using-the-having-clause\" title=\"Anchor for 6) Filtering groups using the HAVING clause\">#<\/a><\/h3>\n\n\n\n<p>To filter groups based on one or more conditions, you use the <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-having\/\">HAVING<\/a><\/code> clause. <\/p>\n\n\n\n<p>For example, the following statement uses the <code>HAVING<\/code> clause to return the city in California, which has more than ten customers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    city,\n    <span class=\"hljs-keyword\">COUNT<\/span> (*)\n<span class=\"hljs-keyword\">FROM<\/span>\n    sales.customers\n<span class=\"hljs-keyword\">WHERE<\/span>\n    state = <span class=\"hljs-string\">'CA'<\/span>\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n    city\n<span class=\"hljs-keyword\">HAVING<\/span>\n    <span class=\"hljs-keyword\">COUNT<\/span> (*) &gt; <span class=\"hljs-number\">10<\/span>\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n    city;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"202\" height=\"114\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-having-clause.png\" alt=\"sql server select - having clause\" class=\"wp-image-166\" srcset=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-having-clause.png 202w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-having-clause-200x114.png 200w\" sizes=\"auto, (max-width: 202px) 100vw, 202px\" \/><\/figure>\n\n\n\n<p>Notice that the <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-where\/\">WHERE<\/a><\/code> clause filters rows while the <code>HAVING<\/code> clause filter groups.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the SQL Server&nbsp;<code>SELECT<\/code> statement to retrieve data from a table.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"102\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/\"\n\t\t\t\tdata-post-title=\"SQL Server SELECT\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"102\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/\"\n\t\t\t\tdata-post-title=\"SQL Server SELECT\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial teaches you how to use the SQL Server SELECT statement to retrieve data from a single table.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":100,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-102","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL Server SELECT<\/title>\n<meta name=\"description\" content=\"This tutorial teaches you how to use the SQL Server SELECT statement to retrieve data from a single table.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Server SELECT\" \/>\n<meta property=\"og:description\" content=\"This tutorial teaches you how to use the SQL Server SELECT statement to retrieve data from a single table.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Server Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-24T13:12:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-customers-table.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-select\\\/\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-select\\\/\",\"name\":\"SQL Server SELECT\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-select\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-select\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/sql-server-select-customers-table.png\",\"datePublished\":\"2018-04-11T15:18:34+00:00\",\"dateModified\":\"2024-09-24T13:12:45+00:00\",\"description\":\"This tutorial teaches you how to use the SQL Server SELECT statement to retrieve data from a single table.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-select\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-select\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-select\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/sql-server-select-customers-table.png\",\"contentUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/sql-server-select-customers-table.png\",\"width\":789,\"height\":208},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/sql-server-select\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server Basics\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-basics\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"SQL Server SELECT\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\",\"name\":\"SQL Server Tutorial\",\"description\":\"The Practical SQL Server Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Server SELECT","description":"This tutorial teaches you how to use the SQL Server SELECT statement to retrieve data from a single table.","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:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/","og_locale":"en_US","og_type":"article","og_title":"SQL Server SELECT","og_description":"This tutorial teaches you how to use the SQL Server SELECT statement to retrieve data from a single table.","og_url":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/","og_site_name":"SQL Server Tutorial","article_modified_time":"2024-09-24T13:12:45+00:00","og_image":[{"url":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-customers-table.png","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/","url":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/","name":"SQL Server SELECT","isPartOf":{"@id":"https:\/\/www.sqlservertutorial.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/#primaryimage"},"image":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-customers-table.png","datePublished":"2018-04-11T15:18:34+00:00","dateModified":"2024-09-24T13:12:45+00:00","description":"This tutorial teaches you how to use the SQL Server SELECT statement to retrieve data from a single table.","breadcrumb":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/#primaryimage","url":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-customers-table.png","contentUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/sql-server-select-customers-table.png","width":789,"height":208},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-select\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlservertutorial.net\/"},{"@type":"ListItem","position":2,"name":"SQL Server Basics","item":"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/"},{"@type":"ListItem","position":3,"name":"SQL Server SELECT"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlservertutorial.net\/#website","url":"https:\/\/www.sqlservertutorial.net\/","name":"SQL Server Tutorial","description":"The Practical SQL Server Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlservertutorial.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":4,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/102\/revisions"}],"predecessor-version":[{"id":4566,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/102\/revisions\/4566"}],"up":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/100"}],"wp:attachment":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/media?parent=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}