02 - advanced select statements
DESCRIPTION
Use of advance SELECT Commands.TRANSCRIPT
SYED SAJID WASIM
Ex-AVP, DHAKABANK Limited
Ex-Sr. Database Engineer, Orion Informatics Limited
Focused on database and development technologies
MCP, MCTS, MCSE on Data Platform & Administration
Working with SQL Server since version 6.5
02 | Advanced SELECT Statements
Querying Microsoft SQL Server 2012 Jump Start
01 | Introducing SQL Server 2012
SQL Server types of statements; other SQL statement elements; basic SELECT statements
02 | Advanced SELECT StatementsDISTINCT, Aliases, scalar functions and CASE, using JOIN and MERGE; Filtering and sorting data, NULL
values
03 | SQL Server Data Types Introduce data types, data type usage, converting data types, understanding SQL Server function types
04 | Grouping and Aggregating DataAggregate functions, GROUP BY and HAVING clauses, subqueries; self-contained, correlated, and EXISTS; Views, inline-table
valued functions, and derived tables
SELECT DISTINCT <column list>
FROM <table or view>
SELECT DISTINCT StoreIDFROM Sales.Customer;
StoreID-------12345709021898710
Using aliases to refer to columns
SELECT SalesOrderID, UnitPrice, OrderQty AS Quantity
FROM Sales.SalesOrderDetail;
SELECT SalesOrderID, UnitPrice, Quantity = OrderQty
FROM Sales.SalesOrderDetail;
SELECT SalesOrderID, UnitPrice Quantity
FROM Sales.SalesOrderDetail;
SELECT SalesOrderID, ProductID
FROM Sales.SalesOrderDetail AS SalesOrders;
SELECT SalesOrderID, ProductID
FROM Sales.SalesOrderDetail SalesOrders;
SELECT SalesOrders.SalesOrderID, SalesOrders.ProductID
FROM Sales.SalesOrderDetail AS SalesOrders;
SELECT ProductID, Name, ProductSubCategoryID,
CASE ProductSubCategoryID
WHEN 1 THEN 'Beverages'
ELSE 'Unknown Category'
END
FROM Production.Product
Keyword Expression component
SELECT <select list>
CASE <value to compare>
WHEN <value to match>
THEN <result>
END N/A
FROM <table source>
Join Type Description
Cross Combines all rows in both tables (creates Cartesian product).
Inner Starts with Cartesian product; applies filter to match rows between
tables based on predicate.
Outer Starts with Cartesian product; all rows from designated table preserved,
matching rows from other table retrieved. Additional NULLs inserted as
placeholders.
SELECT SOH.SalesOrderID, SOH.OrderDate, SOD.ProductID, SOD.UnitPrice, SOD.OrderQty
FROM Sales.SalesOrderHeader AS SOH JOIN Sales.SalesOrderDetail AS SOD ON SOH.SalesOrderID = SOD.SalesOrderID;
FROM t1 JOIN t2 ON t1.column = t2.column
SELECT CUST.CustomerID, CUST.StoreID, ORD.SalesOrderID, ORD.OrderDateFROM Sales.Customer AS CUSTLEFT OUTER JOIN Sales.SalesOrderHeader ASORDON CUST.CustomerID = ORD.CustomerIDWHERE ORD.SalesOrderID IS NULL;
SELECT EMP1.BusinessEntityID, EMP2.JobTitleFROM HumanResources.Employee AS EMP1 CROSS JOIN HumanResources.Employee AS EMP2;
SELECT EMP.EmpID, EMP.LastName,EMP.JobTitle, EMP.MgrID, MGR.LastName
FROM HR.Employees AS EMPINNER JOIN HR.Employees AS MGR ON EMP.MgrID = MGR.EmpID ;
SELECT EMP.EmpID, EMP.LastName,EMP.Title, MGR.MgrID
FROM HumanResources.Employee AS EMPLEFT OUTER JOIN HumanResources.Employee AS MGRON EMP.MgrID = MGR.EmpID;
SELECT SalesOrderID, CustomerID, OrderDateFROM Sales.SalesOrderHeaderORDER BY OrderDate;
SELECT SalesOrderID, CustomerID, YEAR(OrderDate) AS OrderYearFROM Sales.SalesOrderHeaderORDER BY OrderYear;
SELECT SalesOrderID, CustomerID, OrderDateFROM Sales.SalesOrderHeaderORDER BY OrderDate DESC;
WHERE clauses use predicates
Must be expressed as logical conditions
Only rows for which predicate evaluates to TRUE are accepted
Values of FALSE or UNKNOWN are filtered out
WHERE clause follows FROM, precedes other clauses
Can’t see aliases declared in SELECT clause
Can be optimized by SQL Server to use indexes
SELECT CustomerID, TerritoryIDFROM Sales.CustomerWHERE TerritoryID = 6;
SELECT CustomerID, TerritoryIDFROM Sales.CustomerWHERE TerritoryID >= 6;
SELECT CustomerID, TerritoryID, StoreIDFROM Sales.CustomerWHERE StoreID >= 1000 AND StoreID <= 1200;
SELECT TOP (20) SalesOrderID, CustomerID, TotalDueFROM Sales.SalesOrderHeaderORDER BY TotalDue DESC;
SELECT TOP (20) WITH TIES SalesOrderID, CustomerID, TotalDueFROM Sales.SalesOrderHeaderORDER BY TotalDue DESC;
SELECT TOP (1) PERCENT SalesOrderID, CustomerID, TotalDueFROM Sales.SalesOrderHeaderORDER BY TotalDue DESC;
SELECT CustomerID, StoreID, TerritoryIDFROM Sales.CustomerWHERE StoreID IS NULLORDER BY TerritoryID
©2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.