Interrogation after SELECT

Improving Query Performance in SQL Server

Dean Smith

Founder, Atamai Analytics

Processing order after SELECT

1.  FROM

2. ON
3. JOIN
4. WHERE
5. GROUP BY
6. HAVING
7. SELECT
8. DISTINCT
9. ORDER BY
10. TOP
Improving Query Performance in SQL Server

All is not always good

SELECT *
FROM PlayerStats;

SELECT * is great for data interrogation but potentially bad for performance

PlayerName Team Position ... ...
Alex Abrines OKC SG ... ...
Quincy Acy BRK PF ... ...
Steven Adams OKC C ... ...
Bam Adebayo MIA C ... ...
... ... ... ... ...
Improving Query Performance in SQL Server

All you need is better

SELECT PlayerName, 
        Team, 
        TotalPoints
FROM PlayerStats;

Only select the columns required

PlayerName Team TotalPoints
Alex Abrines OKC 353
Quincy Acy BRK 411
Steven Adams OKC 1056
Bam Adebayo MIA 477
... ... ...
Improving Query Performance in SQL Server

All in a JOIN

SELECT * 
FROM Players p
  INNER JOIN PlayerStats ps
  ON ps.PlayerName = p.PlayerName;

SELECT * in joins returns duplicates of joining columns

PlayerName Age PlayerName Team ...
Alex Abrines 24 Alex Abrines OKC ...
Quincy Acy 27 Quincy Acy BRK ...
Steven Adams 24 Steven Adams OKC ...
Bam Adebayo 20 Bam Adebayo MIA ...
... ... ... ... ...
Improving Query Performance in SQL Server

All you need in a JOIN

SELECT p.PlayerName, 
       ps.Team, 
       p.Country, 
       ps.Position
FROM Players p
  INNER JOIN PlayerStats ps
  ON ps.PlayerName = p.PlayerName;

Explicitly state the columns to be returned and from what tables

PlayerName Country Team Position
Alex Abrines Spain OKC SG
Quincy Acy USA BRK PF
Steven Adams New Zealand OKC C
Bam Adebayo USA MIA C
... ... ... ...
Improving Query Performance in SQL Server

Rows at the TOP

SELECT TOP 5 PlayerName, 
        Team, 
        TotalPoints
FROM PlayerStats;
PlayerName Team TotalPoints
Alex Abrines OKC 353
Quincy Acy BRK 411
Steven Adams OKC 1056
Bam Adebayo MIA 477
Arron Afflalo ORL 179
Improving Query Performance in SQL Server

Percentage at the TOP

SELECT TOP 1 PERCENT PlayerName, 
        Team, 
        TotalPoints
FROM PlayerStats;
PlayerName Team TotalPoints
Alex Abrines OKC 353
Quincy Acy BRK 411
Steven Adams OKC 1056
Bam Adebayo MIA 477
Arron Afflalo ORL 179
Cole Aldrich MIN 12
LaMarcus Aldridge SAS 1735
Improving Query Performance in SQL Server

There is no top or bottom

SELECT TOP 5 PlayerName, 
        Team, 
        TotalPoints
FROM PlayerStats
ORDER BY TotalPoints DESC
PlayerName Team TotalPoints
LeBron James CLE 2251
James Harden HOU 2191
Anthony Davis NOP 2110
Russell Westbrook OKC 2028
Giannis Antetokounmpo MIL 2014
Improving Query Performance in SQL Server

The other row limiters

Row limiter Ordering clause Database
TOP ORDER BY Microsoft SQL Server
ROWNUM ORDER BY Oracle
LIMIT ORDER BY PostgreSQL
Improving Query Performance in SQL Server

Where to use ORDER BY

Use ORDER BY in a query:

  • To interrogate the data
  • When there is a good reason for the final returned results to be sorted
Improving Query Performance in SQL Server

Let's practice!

Improving Query Performance in SQL Server

Preparing Video For Download...