{"id":2318,"date":"2014-02-08T03:15:43","date_gmt":"2014-02-07T21:45:43","guid":{"rendered":"http:\/\/sqlhints.com\/?p=2318"},"modified":"2014-02-08T03:15:43","modified_gmt":"2014-02-07T21:45:43","slug":"querying-data-using-select-in-sql-server","status":"publish","type":"post","link":"https:\/\/sqlhints.com\/2014\/02\/08\/querying-data-using-select-in-sql-server\/","title":{"rendered":"Querying Data Using SELECT In Sql Server"},"content":{"rendered":"<h2><a title=\"Sql Server Tutorial\" href=\"https:\/\/sqlhints.com\/tutorialsqlserver\/\" target=\"_blank\">Sql Server Tutorial<\/a>\u00a0Lesson 5: Querying Data Using SELECT<\/h2>\n<p style=\"text-align: justify;\">SELECT is one of the basic construct of Sql Server, which basically facilitates the retrieval of information from Tables. This lesson covers the following Topics with extensive list of real-time examples.<\/p>\n<ol>\n<li>Introduction to <strong>SELECT<\/strong> Statement<\/li>\n<li>Using <strong>WHERE<\/strong> Clause<\/li>\n<li>Using Boolean Operators <strong>AND<\/strong>, <strong>OR<\/strong> and <strong>NOT<\/strong><\/li>\n<li>Using <strong>LIKE<\/strong> Predicate<\/li>\n<li>Using <strong>BETWEEN<\/strong> Clause<\/li>\n<li>Table and Column Name <strong>Alias<\/strong><\/li>\n<li>Using <strong>ORDER BY<\/strong> Clause<\/li>\n<li><strong>Concatenation<\/strong><\/li>\n<\/ol>\n<p style=\"text-align: justify;\">To demo these features let us first create the Employee table with seven employee records as depicted in the below image by using the following script:<\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/QueryingDataUsingSELECT.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2360\" alt=\"QueryingDataUsingSELECT\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/QueryingDataUsingSELECT.jpg\" width=\"608\" height=\"193\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/QueryingDataUsingSELECT.jpg 608w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/QueryingDataUsingSELECT-300x95.jpg 300w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/QueryingDataUsingSELECT-604x193.jpg 604w\" sizes=\"auto, (max-width: 608px) 100vw, 608px\" \/><\/a><\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">CREATE DATABASE SqlHintsQueryingDataDemo\r\nGO\r\nUSE SqlHintsQueryingDataDemo\r\nGO\r\nCREATE TABLE dbo.Employee\r\n(EmployeeId INT, Name NVARCHAR(50),\r\nDOJ DateTime,City NVarchar(50), Salary Money)\r\nGO\r\nINSERT INTO dbo.Employee VALUES(1,&#039;ShreeGanesh Biradar&#039;,&#039;2011\/12\/18&#039;,&#039;Pune&#039;,45000)\r\nINSERT INTO dbo.Employee VALUES(2,&#039;Sandeep Patil&#039;,&#039;2010\/02\/24&#039;,NULL,55000)\r\nINSERT INTO dbo.Employee VALUES(3,&#039;Abhi Akkanna&#039;,&#039;2008\/03\/22&#039;,&#039;Bangalore&#039;,89000)\r\nINSERT INTO dbo.Employee VALUES(4,&#039;Sandy Thomas&#039;,&#039;2008\/04\/28&#039;,&#039;Delhi&#039;,39000)\r\nINSERT INTO dbo.Employee VALUES(5,&#039;Kalpana Biradar&#039;,&#039;2013\/11\/15&#039;,&#039;Bangalore&#039;,60000)\r\nINSERT INTO dbo.Employee VALUES(6,&#039;Basav Biradar&#039;,&#039;2012\/11\/15&#039;,&#039;Bangalore&#039;,54000)\r\nINSERT INTO dbo.Employee VALUES(7,&#039;Deepak Kumar&#039;,&#039;2006\/04\/08&#039;,&#039;Hyderabad&#039;,75000)<\/pre>\n<h2>1. Introduction to SELECT Statement<\/h2>\n<p style=\"text-align: justify;\"><strong>Demo 1: Retrieve All the Records from the Employee Table<\/strong><\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT * FROM dbo.Employee<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/QueryingDataUsingSELECTDemo1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2364\" alt=\"QueryingDataUsingSELECTDemo1\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/QueryingDataUsingSELECTDemo1.jpg\" width=\"494\" height=\"220\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/QueryingDataUsingSELECTDemo1.jpg 494w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/QueryingDataUsingSELECTDemo1-300x133.jpg 300w\" sizes=\"auto, (max-width: 494px) 100vw, 494px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><strong>Demo 2: Retrieve only the Required Information<\/strong><\/p>\n<p style=\"text-align: justify;\">In most of the scenario&#8217;s we don&#8217;t need the complete table data, so it is always best practice to include the columns which are required in the Select query to reduce unnecessary data transfer over the Network and IO&#8217;s.<\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT EmployeeId,Name,City FROM dbo.Employee<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/QueryingDataUsingSELECTDemo2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2365\" alt=\"QueryingDataUsingSELECTDemo2\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/QueryingDataUsingSELECTDemo2.jpg\" width=\"359\" height=\"222\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/QueryingDataUsingSELECTDemo2.jpg 359w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/QueryingDataUsingSELECTDemo2-300x185.jpg 300w\" sizes=\"auto, (max-width: 359px) 100vw, 359px\" \/><\/a><\/p>\n<h2>2. Using WHERE Clause<\/h2>\n<p><strong>Demo 1: Get all the Employees whose City is <em>Bangalore<\/em><\/strong><\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT * FROM dbo.Employee WHERE City = &#039;Bangalore&#039;<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingWHEREClause1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2367\" alt=\"UsingWHEREClause1\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingWHEREClause1.jpg\" width=\"462\" height=\"142\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingWHEREClause1.jpg 462w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingWHEREClause1-300x92.jpg 300w\" sizes=\"auto, (max-width: 462px) 100vw, 462px\" \/><\/a><\/p>\n<p><strong>Demo 2: Get all the Employees whose city is <em>not Bangalore<\/em><\/strong><\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT * FROM dbo.Employee WHERE City &lt;&gt; &#039;Bangalore&#039;<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingWHEREClause4.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2370\" alt=\"UsingWHEREClause4\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingWHEREClause4.jpg\" width=\"494\" height=\"139\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingWHEREClause4.jpg 494w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingWHEREClause4-300x84.jpg 300w\" sizes=\"auto, (max-width: 494px) 100vw, 494px\" \/><\/a><\/p>\n<p style=\"text-align: justify; color: red;\"><em>Note: This query ignored the employees whose City column value is NULL, because NULL value can&#8217;t be compared with some value (i.e. Null means unknown value, so it can&#8217;t be used to compare with any know values). \u00a0The only operation we can do with NULL is we can check whether it is NULL or NOT NULL as shown in the below Demos 3 an 4.<\/em><\/p>\n<p style=\"text-align: justify;\"><strong>Demo 3: Get all the Employees whose city column has some value (i.e. employees whose City column value is NOT NULL)<\/strong><\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT * FROM dbo.Employee WHERE City is NOT NULL<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingWHEREClause2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2368\" alt=\"UsingWHEREClause2\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingWHEREClause2.jpg\" width=\"498\" height=\"204\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingWHEREClause2.jpg 498w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingWHEREClause2-300x122.jpg 300w\" sizes=\"auto, (max-width: 498px) 100vw, 498px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><strong>Demo 4: Get all the Employees whose city column value is null<\/strong><\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT * FROM dbo.Employee WHERE City is NULL<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT: <\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingWHEREClause3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2369\" alt=\"UsingWHEREClause3\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingWHEREClause3.jpg\" width=\"430\" height=\"108\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingWHEREClause3.jpg 430w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingWHEREClause3-300x75.jpg 300w\" sizes=\"auto, (max-width: 430px) 100vw, 430px\" \/><\/a><\/p>\n<h2>3. Using Boolean Operators AND, OR and NOT<\/h2>\n<p style=\"text-align: justify;\"><strong>Demo 1 Using Boolean Operator <em>AND<\/em>:<\/strong> Get all the Employees whose City is Bangalore and Salary is above 55000<\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT * FROM dbo.Employee \r\nWHERE City = &#039;Bangalore&#039; AND Salary &gt; 55000<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingBooleanOperatorAND.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2371\" alt=\"UsingBooleanOperatorAND\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingBooleanOperatorAND.jpg\" width=\"527\" height=\"133\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingBooleanOperatorAND.jpg 527w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingBooleanOperatorAND-300x75.jpg 300w\" sizes=\"auto, (max-width: 527px) 100vw, 527px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><strong>Demo 2 Using Boolean Operator <em>OR<\/em>:\u00a0<\/strong>Get all the Employees whose city is either Bangalore or have salary above 62000<\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT * FROM dbo.Employee \r\nWHERE City = &#039;Bangalore&#039; OR Salary &gt; 62000<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingBooleanOperatorOR.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2372\" alt=\"UsingBooleanOperatorOR\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingBooleanOperatorOR.jpg\" width=\"525\" height=\"164\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingBooleanOperatorOR.jpg 525w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingBooleanOperatorOR-300x93.jpg 300w\" sizes=\"auto, (max-width: 525px) 100vw, 525px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><strong>Demo 3 Using Boolean Operator <em>NOT<\/em>:<\/strong> Get all the Employees whose city is other than Bangalore and Hyderabad<\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT * FROM dbo.Employee \r\nWHERE City NOT IN (&#039;Bangalore&#039;,&#039;Hyderabad&#039;)<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingBooleanOperatorNOT.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2373\" alt=\"UsingBooleanOperatorNOT\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingBooleanOperatorNOT.jpg\" width=\"529\" height=\"121\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingBooleanOperatorNOT.jpg 529w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingBooleanOperatorNOT-300x68.jpg 300w\" sizes=\"auto, (max-width: 529px) 100vw, 529px\" \/><\/a><\/p>\n<p style=\"text-align: justify; color: red;\"><em>Note: The above query didn&#8217;t return the employees whose is CITY column value is NULL. As explained previously NULL means unknown value, it can&#8217;t be compared with a known value.<\/em><\/p>\n<h2>4. Using LIKE Predicate<\/h2>\n<p style=\"text-align: justify;\"><strong>Demo 1: Get all the Employees who have word <em>deep<\/em> anywhere in their Name.<\/strong><\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT * FROM dbo.Employee WHERE Name Like &#039;%deep%&#039;<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/LIKE1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2409\" alt=\"LIKE1\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/LIKE1.jpg\" width=\"468\" height=\"130\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/LIKE1.jpg 468w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/LIKE1-300x83.jpg 300w\" sizes=\"auto, (max-width: 468px) 100vw, 468px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><strong>Demo 2: Get all the Employees whose Name starts with the word <em>dee<\/em><\/strong><\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT * FROM dbo.Employee WHERE Name Like &#039;deep%&#039;<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/LIKE2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2410\" alt=\"LIKE2\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/LIKE2.jpg\" width=\"469\" height=\"110\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/LIKE2.jpg 469w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/LIKE2-300x70.jpg 300w\" sizes=\"auto, (max-width: 469px) 100vw, 469px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><strong>Demo 3: Get all the Employees whose Name starts with the\u00a0character\u00a0<em>a<\/em> or <em>b<\/em> or <em>c<\/em><\/strong>.<br \/>\nNote the below three SELECT statements are equivalent<\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT * FROM dbo.Employee WHERE Name like &#039;[a-c]%&#039;\r\nSELECT * FROM dbo.Employee WHERE Name like &#039;[abc]%&#039;\r\nSELECT * FROM dbo.Employee WHERE Name like &#039;[a,b,c]%&#039;<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/LIKE3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2412\" alt=\"LIKE3\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/LIKE3.jpg\" width=\"460\" height=\"309\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/LIKE3.jpg 460w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/LIKE3-300x201.jpg 300w\" sizes=\"auto, (max-width: 460px) 100vw, 460px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><strong>Demo 4: Get all the Employees whose Name starts with the\u00a0character\u00a0<em>a<\/em> or <em>b<\/em> or <em>c<\/em> and second\u00a0character must be <em>a<\/em><\/strong><\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT * FROM dbo.Employee WHERE Name like &#039;[a-c][a]%&#039;<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/LIKE4.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2411\" alt=\"LIKE4\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/LIKE4.jpg\" width=\"461\" height=\"113\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/LIKE4.jpg 461w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/LIKE4-300x73.jpg 300w\" sizes=\"auto, (max-width: 461px) 100vw, 461px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><strong>Demo 5: Get all the Employees whose Name is not starting with letter <em>a<\/em> or <em>b<\/em> or <em>c<\/em><\/strong><\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT * FROM dbo.Employee WHERE Name like &#039;[^a-c]%&#039;<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/LIKE5.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2413\" alt=\"LIKE5\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/LIKE5.jpg\" width=\"500\" height=\"187\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/LIKE5.jpg 500w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/LIKE5-300x112.jpg 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<h2>5. Using BETWEEN Clause<\/h2>\n<p style=\"text-align: justify;\">BETWEEN clause can be used to compare range of values.<\/p>\n<p style=\"text-align: justify;\"><strong>Demo 1: Get all the Employees whose Salary is between 45000 to 60000.<\/strong><\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT * FROM dbo.Employee\r\nWHERE Salary between 45000 AND 60000<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingBETWEENClause1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2375\" alt=\"UsingBETWEENClause1\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingBETWEENClause1.jpg\" width=\"493\" height=\"164\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingBETWEENClause1.jpg 493w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingBETWEENClause1-300x99.jpg 300w\" sizes=\"auto, (max-width: 493px) 100vw, 493px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><strong>Demo 2: Above <em>Demo 1<\/em> query can also be written without between Clause as below:<\/strong><\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT * FROM dbo.Employee \r\nWHERE Salary &gt;= 45000 AND Salary &lt;= 60000<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingBETWEENClause2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2376\" alt=\"UsingBETWEENClause2\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingBETWEENClause2.jpg\" width=\"489\" height=\"176\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingBETWEENClause2.jpg 489w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingBETWEENClause2-300x107.jpg 300w\" sizes=\"auto, (max-width: 489px) 100vw, 489px\" \/><\/a><\/p>\n<h2>6. Table and Column Name Alias<\/h2>\n<p style=\"text-align: justify;\">Sql Server provides an option to give temporary alias name for the Table and it&#8217;s Column Names in the query. In that way we can give an meaning full alias to the Tables. And if two tables are joined both have the same column name in it, then we have to write two part column names i.e. [Table Name].[Column Name] otherwise Sql server gives an ambiguous column name error. If table name is too long it looks to clumsy, so better give a short alias name for the table and use this alias table name in the Two part column name specification to avoid ambiguity.<\/p>\n<p style=\"text-align: justify;\"><strong>Demo 1: Table Alias Name demo.<\/strong>\u00a0In this demo example for Employee Table the alias name specified is E in the FROM clause. Because of this we can access the employee table column names by prefixing <em><strong>E.\u00a0<\/strong><\/em><\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT E.Name, E.City FROM dbo.Employee E<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/TableAndColumnNameAlias1.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/TableAndColumnNameAlias1.jpg\" alt=\"TableAndColumnNameAlias1\" width=\"326\" height=\"229\" class=\"alignnone size-full wp-image-2421\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/TableAndColumnNameAlias1.jpg 326w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/TableAndColumnNameAlias1-300x210.jpg 300w\" sizes=\"auto, (max-width: 326px) 100vw, 326px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><strong>Demo 2: Table and Column Name Alias demo.<\/strong>\u00a0In this demo example for Employee Table the alias name specified is E in the FROM clause. Because of this we can access the employee table column names by prefixing\u00a0<em><strong>E. .\u00a0<\/strong><\/em>And for the Name column we are specifying the alias name as &#8216;Employee Name&#8217; and for City column the alias name is &#8216;Employee City&#8217;<\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT E.Name AS &#039;Employee Name&#039;, E.City AS [Employee City]\r\nFROM dbo.Employee AS E<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/TableAndColumnNameAlias2.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/TableAndColumnNameAlias2.jpg\" alt=\"TableAndColumnNameAlias2\" width=\"454\" height=\"231\" class=\"alignnone size-full wp-image-2423\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/TableAndColumnNameAlias2.jpg 454w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/TableAndColumnNameAlias2-300x152.jpg 300w\" sizes=\"auto, (max-width: 454px) 100vw, 454px\" \/><\/a><\/p>\n<h2>7. Using ORDER BY Clause<\/h2>\n<p><strong>ORDER BY<\/strong> Clause can be used to sort the result set based on the Column Value.<\/p>\n<p style=\"text-align: justify;\"><strong>Demo 1: Sort the Employee records based on Name column value.<\/strong> The default sorting of the ORDER BY clause is in the Ascending Orders.<\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT Name, City FROM dbo.Employee\r\nORDER BY Name<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingOrderByClause4.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2379\" alt=\"UsingOrderByClause4\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingOrderByClause4.jpg\" width=\"293\" height=\"231\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><strong>Demo 2: Sort the Employee records based on Name column value in the descending order.<\/strong> Here using the keyword DESC in conjunction with ORDER By clause to sort the records by Name in Descending order.<\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT Name, City FROM dbo.Employee\r\nORDER BY Name DESC<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingOrderByClause2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2380\" alt=\"UsingOrderByClause2\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/UsingOrderByClause2.jpg\" width=\"289\" height=\"230\" \/><\/a><\/p>\n<h2>8. Concatenation<\/h2>\n<p>&#8216;+&#8217; symbol \u00a0is used for concatenating string values<\/p>\n<p><strong>Demo 1: Concatenate Name and City Column Value.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT Name + City as [Name &amp; City]\r\nFROM dbo.Employee<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/ConcateNation1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2381\" alt=\"ConcateNation1\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/ConcateNation1.jpg\" width=\"414\" height=\"218\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/ConcateNation1.jpg 414w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/ConcateNation1-300x157.jpg 300w\" sizes=\"auto, (max-width: 414px) 100vw, 414px\" \/><\/a><\/p>\n<p style=\"text-align: justify; color: red;\"><em>Note: \u00a0If &#8216;+&#8217; symbol is used to concatenate the values, then if one of the values is NULL then resultant concatenated value will also be NULL.<br \/>\n<\/em><\/p>\n<p style=\"text-align: justify;\"><strong>Demo 2: One way of avoiding NULL as the RESULT of concatenation if one of the value of the to be concatenated is NULL is to use the ISNULL function like below. Here ISNULL function returns an empty string if the value is NULL otherwise the specified value.<\/strong><\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT ISNULL(Name,&#039;&#039;) + ISNULL(City,&#039;&#039;) AS [Name &amp; City]\r\nFROM dbo.Employee<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/ConcateNation6.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2405\" alt=\"ConcateNation6\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/ConcateNation6.jpg\" width=\"443\" height=\"233\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/ConcateNation6.jpg 443w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/ConcateNation6-300x157.jpg 300w\" sizes=\"auto, (max-width: 443px) 100vw, 443px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\"><strong>Demo 3: Add an empty space between Name and City.<\/strong><\/p>\n<pre class=\"brush: sql; gutter: false; first-line: 1; highlight: []; html-script: false\">SELECT ISNULL(Name,&#039;&#039;) + &#039; &#039; + ISNULL(City,&#039;&#039;) AS [Name &amp; City]\r\nFROM dbo.Employee<\/pre>\n<p style=\"text-align: justify;\"><strong>RESULT:<\/strong><\/p>\n<p><a href=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/ConcateNation5.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2404\" alt=\"ConcateNation5\" src=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/ConcateNation5.jpg\" width=\"482\" height=\"235\" srcset=\"https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/ConcateNation5.jpg 482w, https:\/\/sqlhints.com\/wp-content\/uploads\/2014\/02\/ConcateNation5-300x146.jpg 300w\" sizes=\"auto, (max-width: 482px) 100vw, 482px\" \/><\/a><\/p>\n<p>You may like to go through the string function <a title=\"CONCAT() STRING FUNCTION IN SQL Server 2012\" href=\"https:\/\/sqlhints.com\/2012\/12\/02\/concat-function-in-sql-server-2012\/\" target=\"_blank\">CONCAT()<\/a> which is introduced in Sql Server 2012 for concatenation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sql Server Tutorial\u00a0Lesson 5: Querying Data Using SELECT SELECT is one of the basic construct of Sql Server, which basically facilitates the retrieval of information from Tables. This lesson covers the following Topics with extensive list of real-time examples. Introduction to SELECT Statement Using WHERE Clause Using Boolean Operators AND, OR and NOT Using LIKE &hellip; <a href=\"https:\/\/sqlhints.com\/2014\/02\/08\/querying-data-using-select-in-sql-server\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Querying Data Using SELECT In Sql Server<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[3,242],"tags":[292,289,297,295,291,290,294,287,38,986,996,296,293,288],"class_list":["post-2318","post","type-post","status-publish","format-standard","hentry","category-sql-server","category-sql-server-tutorial","tag-between-clause","tag-boolean-operators-and","tag-column-name-alias","tag-concatenation","tag-like-predicate","tag-or-and-not","tag-order-by-clause","tag-querying-data-using-select-in-sql-server","tag-select","tag-sql-server","tag-sql-server-tutorial","tag-table-alias","tag-table-and-column-name-alias","tag-where-clause"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3xNAz-Bo","_links":{"self":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/2318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/comments?post=2318"}],"version-history":[{"count":42,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/2318\/revisions"}],"predecessor-version":[{"id":5784,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/2318\/revisions\/5784"}],"wp:attachment":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/media?parent=2318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/categories?post=2318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/tags?post=2318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}