SlideShare a Scribd company logo
SQL Server 2016 Queries
Seyed Ibrahim
Alias
• Column Alias
• When the column names are long
• More than one columns are to be combined
• SELECT FirstName As 'First Name', LastName AS 'Surname'
FROM StudentDetails;
• SELECT first_name FName FROM StudentDetails;
• Table Alias
• Common in Joins
• SELCT … FROM dbname.schemaname.tablename AS Aliasname
Operators
• Comparison Operators
• =
• <>, !=
• <
• >
• >=
• <=
• Logical Operators
• AND
• OR
• NOT
SQL Comparison Keywords
Comparison Operators Description
LIKE column value is similar to specified character(s).
IN column value is equal to any one of a specified values.
BETWEEN...AND column value is between two values, including the border values
IS NULL Check if the column is NULL
ROWCOUNT
This statement will limit the number of rows that will be
processed for the session in which the statement is executed
SET ROWCOUNT 3
SELECT * FROM ShareDetails.Shares
SET ROWCOUNT 0
SELECT * FROM ShareDetails.Shares
WHERE
• WHERE {column or expression} comparison-operator value
SELECT name, salary, salary*1.2 AS NewSalary
FROM dbo.employee
WHERE salary * 1.2 >= 80000 -- salary * 1.2 is an expression
SELECT name, salary, salary*1.2 AS NewSalary
FROM dbo.employee
WHERE salary < 80000 AND (grade BETWEEN 1 AND 5)
GROUP BY & HAVING
• GROUP BY: Groups a selected set of rows into a set of summary rows by the
values of one or more columns
SELECT dept_id, MIN(salary) AS LowestSalary
FROM dbo.employees
GROUP BY dept_id
• HAVING: Specifies a search condition for a group or an aggregate
SELECT SalesOrderID, SUM(LineTotal) AS SubTotal
FROM Sales.SalesOrderDetail
GROUP BY SalesOrderID
HAVING SUM(LineTotal) > 100000.00
ORDER BY SalesOrderID
ORDER BY
• There are no pointers, or Row numbers in RDBMS
• It is possible to change the order in which rows are returned
• ORDER BY clause can have multiple columns, with some in ascending order
and others in descending order
• ORDER BY Balance DESC
• ORDER BY name, salary
• ORDER BY 2,4
Subqueries
• a query embedded within another query or statement
SELECT *
FROM ShareDetails.Shares
WHERE ShareId IN (1,11,15)  Not a Subquery
• A Simple SubQuery using IN
SELECT id, first_name
FROM student_details
WHERE id IN (SELECT student_id
FROM student_subjects
WHERE subject= 'Science')
Correlated Subquery
• a sub-query that uses values from the outer query. In this case the inner query
has to be executed for every row of outer query
• May be inefficient
• E.g.: find all employees whose salary is above average for their department
SELECT employee_number, name
FROM employees AS emp
WHERE salary > (
SELECT AVG(salary)
FROM employees
WHERE department = emp.department);
What does this do?
SELECT orderid, custid
FROM Sales.Orders AS O1
WHERE orderid = (SELECT MAX(O2.orderid)
FROM Sales.Orders AS O2
WHERE O2.custid = O1.custid);
Exercise: Convert the above query to an efficient query

More Related Content

PPT
SQL select statement and functions
PPT
Sql server select queries ppt 18
PPT
Retrieving data using the sql select statement
PPTX
Where conditions and Operators in SQL
PPTX
DB2 Sql Query
PPT
Sql select
PPTX
Structure query language (sql)
PPTX
Introduction to SQL
SQL select statement and functions
Sql server select queries ppt 18
Retrieving data using the sql select statement
Where conditions and Operators in SQL
DB2 Sql Query
Sql select
Structure query language (sql)
Introduction to SQL

What's hot (20)

PPT
Advanced Sql Training
PPTX
1. dml select statement reterive data
PPTX
Basic SQL Statments
PPT
Les01 (retrieving data using the sql select statement)
PPTX
Sql subquery
PPTX
Using basic select statement in oracle database
PPT
Sql 2006
PDF
Basics of SELECT Statement - Oracle SQL
PDF
Updat Dir
PPTX
Advanced SQL Webinar
PPTX
Introduction to SQL
PPT
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
PDF
Database Management Essentials: Module 4 Basic Query Formulation with SQL
PPTX
Lab1 select statement
PPTX
Database constraints
PPT
SQL subquery
PPT
ALL ABOUT SQL AND RDBMS
PPTX
Intro to t sql – 3rd session
PPTX
MySQL index optimization techniques
Advanced Sql Training
1. dml select statement reterive data
Basic SQL Statments
Les01 (retrieving data using the sql select statement)
Sql subquery
Using basic select statement in oracle database
Sql 2006
Basics of SELECT Statement - Oracle SQL
Updat Dir
Advanced SQL Webinar
Introduction to SQL
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
Database Management Essentials: Module 4 Basic Query Formulation with SQL
Lab1 select statement
Database constraints
SQL subquery
ALL ABOUT SQL AND RDBMS
Intro to t sql – 3rd session
MySQL index optimization techniques
Ad

Similar to Sql server 2016 queries (20)

PDF
SQL Lesson 6 - Select.pdf
PDF
0808.pdf
PDF
0808.pdf
PPSX
MS SQL Server
PDF
Structure query language, database course
PDF
Database Systems - SQL - DDL Statements (Chapter 3/3)
PPT
SQL.ppt
PPTX
MySQL basics
PPT
Chinabankppt
PPTX
PPTX
Modul_Pratikum_SQL_Seleksi_Dataaaaa.pptx
PDF
DP080_Lecture_1 SQL lecture document .pdf
PPTX
Data Manipulation Language.pptx
PPT
asdasdasdasdsadasdasdasdasdsadasdasdasdsadsadasd
PPTX
ms-sql-server-150223140402-conversion-gate02.pptx
PDF
Complete SQL Tutorial In Hindi By Rishabh Mishra (Basic to Advance).pdf
PPTX
Episode 4 - Introduction to SOQL in Salesforce
PPTX
PPTX
SQL data types: INT, VARCHAR, CHAR,.pptx
ODP
Ms sql-server
SQL Lesson 6 - Select.pdf
0808.pdf
0808.pdf
MS SQL Server
Structure query language, database course
Database Systems - SQL - DDL Statements (Chapter 3/3)
SQL.ppt
MySQL basics
Chinabankppt
Modul_Pratikum_SQL_Seleksi_Dataaaaa.pptx
DP080_Lecture_1 SQL lecture document .pdf
Data Manipulation Language.pptx
asdasdasdasdsadasdasdasdasdsadasdasdasdsadsadasd
ms-sql-server-150223140402-conversion-gate02.pptx
Complete SQL Tutorial In Hindi By Rishabh Mishra (Basic to Advance).pdf
Episode 4 - Introduction to SOQL in Salesforce
SQL data types: INT, VARCHAR, CHAR,.pptx
Ms sql-server
Ad

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
System and Network Administraation Chapter 3
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
ai tools demonstartion for schools and inter college
PDF
Digital Strategies for Manufacturing Companies
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Nekopoi APK 2025 free lastest update
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
System and Network Administration Chapter 2
PDF
top salesforce developer skills in 2025.pdf
PPTX
history of c programming in notes for students .pptx
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
How to Choose the Right IT Partner for Your Business in Malaysia
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
System and Network Administraation Chapter 3
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Upgrade and Innovation Strategies for SAP ERP Customers
Navsoft: AI-Powered Business Solutions & Custom Software Development
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
ai tools demonstartion for schools and inter college
Digital Strategies for Manufacturing Companies
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Nekopoi APK 2025 free lastest update
Odoo Companies in India – Driving Business Transformation.pdf
System and Network Administration Chapter 2
top salesforce developer skills in 2025.pdf
history of c programming in notes for students .pptx
wealthsignaloriginal-com-DS-text-... (1).pdf

Sql server 2016 queries

  • 1. SQL Server 2016 Queries Seyed Ibrahim
  • 2. Alias • Column Alias • When the column names are long • More than one columns are to be combined • SELECT FirstName As 'First Name', LastName AS 'Surname' FROM StudentDetails; • SELECT first_name FName FROM StudentDetails; • Table Alias • Common in Joins • SELCT … FROM dbname.schemaname.tablename AS Aliasname
  • 3. Operators • Comparison Operators • = • <>, != • < • > • >= • <= • Logical Operators • AND • OR • NOT
  • 4. SQL Comparison Keywords Comparison Operators Description LIKE column value is similar to specified character(s). IN column value is equal to any one of a specified values. BETWEEN...AND column value is between two values, including the border values IS NULL Check if the column is NULL
  • 5. ROWCOUNT This statement will limit the number of rows that will be processed for the session in which the statement is executed SET ROWCOUNT 3 SELECT * FROM ShareDetails.Shares SET ROWCOUNT 0 SELECT * FROM ShareDetails.Shares
  • 6. WHERE • WHERE {column or expression} comparison-operator value SELECT name, salary, salary*1.2 AS NewSalary FROM dbo.employee WHERE salary * 1.2 >= 80000 -- salary * 1.2 is an expression SELECT name, salary, salary*1.2 AS NewSalary FROM dbo.employee WHERE salary < 80000 AND (grade BETWEEN 1 AND 5)
  • 7. GROUP BY & HAVING • GROUP BY: Groups a selected set of rows into a set of summary rows by the values of one or more columns SELECT dept_id, MIN(salary) AS LowestSalary FROM dbo.employees GROUP BY dept_id • HAVING: Specifies a search condition for a group or an aggregate SELECT SalesOrderID, SUM(LineTotal) AS SubTotal FROM Sales.SalesOrderDetail GROUP BY SalesOrderID HAVING SUM(LineTotal) > 100000.00 ORDER BY SalesOrderID
  • 8. ORDER BY • There are no pointers, or Row numbers in RDBMS • It is possible to change the order in which rows are returned • ORDER BY clause can have multiple columns, with some in ascending order and others in descending order • ORDER BY Balance DESC • ORDER BY name, salary • ORDER BY 2,4
  • 9. Subqueries • a query embedded within another query or statement SELECT * FROM ShareDetails.Shares WHERE ShareId IN (1,11,15)  Not a Subquery • A Simple SubQuery using IN SELECT id, first_name FROM student_details WHERE id IN (SELECT student_id FROM student_subjects WHERE subject= 'Science')
  • 10. Correlated Subquery • a sub-query that uses values from the outer query. In this case the inner query has to be executed for every row of outer query • May be inefficient • E.g.: find all employees whose salary is above average for their department SELECT employee_number, name FROM employees AS emp WHERE salary > ( SELECT AVG(salary) FROM employees WHERE department = emp.department);
  • 11. What does this do? SELECT orderid, custid FROM Sales.Orders AS O1 WHERE orderid = (SELECT MAX(O2.orderid) FROM Sales.Orders AS O2 WHERE O2.custid = O1.custid); Exercise: Convert the above query to an efficient query