Improving Query Performance in SQL Server
Dean Smith
Founder, Atamai Analytics
Select ps.Team, count(p.PlayerName)
As NonNthAmerPlayers from
PlayerStats ps inner
join (select PlayerName FROM Players
WHERE Country <> 'USA' Or Country
<> 'Canada' )
p on p.PlayerName = ps.PlayerName
group BY ps.Team
having Count(p.PlayerName)
>=24 Order by NonNthAmerPlayers desc
Team | NonNthAmerPlayers |
---|---|
HOU | 24 |
LAL | 24 |
MEM | 24 |
MIL | 24 |
SELECT
, FROM
, WHERE
, etc.ON
statementsAND
/OR
conditionsAS
From
Select ps.Team, count(p.PlayerName)
As NonNthAmerPlayers from
PlayerStats ps inner
join (select PlayerName FROM Players
WHERE Country <> 'USA' Or Country
<> 'Canada' )
p on p.PlayerName = ps.PlayerName
group BY ps.Team
having Count(p.PlayerName)
>=24 Order by NonNthAmerPlayers desc
To
SELECT ps.Team,
COUNT(p.PlayerName) NonNthAmerPlayers
FROM PlayerStats ps
INNER JOIN
(SELECT PlayerName
FROM Players
WHERE Country <> 'USA'
OR Country <> 'Canada' ) p
ON p.PlayerName = ps.PlayerName
GROUP BY ps.Team
HAVING COUNT(p.PlayerName) >=24
ORDER BY NonNthAmerPlayers DESC;
/* Returns a list of NBA teams with 24 or more non-North American players on the team roster. */
SELECT ps.Team, COUNT(p.PlayerName) NonNthAmerPlayers FROM PlayerStats ps INNER JOIN (SELECT PlayerName FROM Players WHERE Country <> 'USA' OR Country <> 'Canada' ) p ON p.PlayerName = ps.PlayerName GROUP BY ps.Team HAVING COUNT(p.PlayerName) >=24 ORDER BY NonNthAmerPlayers DESC;
Use /*
and */
to comment out a block of code or text
/* Returns a list of NBA teams with 24 or more non-North American players on the team roster. */
SELECT ps.Team, COUNT(p.PlayerName) NonNthAmerPlayers FROM PlayerStats ps INNER JOIN (SELECT PlayerName FROM Players WHERE Country <> 'USA' OR Country <> 'Canada' ) p ON p.PlayerName = ps.PlayerName GROUP BY ps.Team HAVING COUNT(p.PlayerName) >=24 ORDER BY NonNthAmerPlayers DESC;
Use /*
and */
to comment out a block of code or text
Team | NonNthAmerPlayers |
---|---|
HOU | 24 |
LAL | 24 |
MEM | 24 |
MIL | 24 |
Use -- to comment out a single line of code or text
SELECT ps.Team,
COUNT(p.PlayerName) NonNthAmerPlayers
FROM PlayerStats ps
INNER JOIN
(SELECT PlayerName
FROM Players
WHERE Country <> 'USA'
OR Country <> 'Canada' ) p
ON p.PlayerName = ps.PlayerName
GROUP BY ps.Team
HAVING COUNT(p.PlayerName) >=24
ORDER BY NonNthAmerPlayers DESC;
Use -- to comment out a single line of code or text
SELECT ps.Team,
COUNT(p.PlayerName) NonNthAmerPlayers -- Count of players
FROM PlayerStats ps
INNER JOIN
(SELECT PlayerName
FROM Players
WHERE Country <> 'USA'
OR Country <> 'Canada' ) p -- Indented qub-suery
ON p.PlayerName = ps.PlayerName
GROUP BY ps.Team
HAVING COUNT(p.PlayerName) >=24
ORDER BY NonNthAmerPlayers DESC;
Use -- to comment out a single line of code or text
SELECT ps.Team,
COUNT(p.PlayerName) NonNthAmerPlayers -- Count of players
FROM PlayerStats ps
-- Inner join starts here
INNER JOIN
(SELECT PlayerName
FROM Players
WHERE Country <> 'USA'
OR Country <> 'Canada' ) p -- Indented qub-suery
ON p.PlayerName = ps.PlayerName
GROUP BY ps.Team
HAVING COUNT(p.PlayerName) >=24
-- Remove the ORDER BY, it is not required
ORDER BY NonNthAmerPlayers DESC;
INNER JOIN
ORDER BY
Use -- to comment out a single line of code or text
SELECT ps.Team,
COUNT(p.PlayerName) NonNthAmerPlayers -- Count of players
FROM PlayerStats ps
-- Inner join starts here
INNER JOIN
(SELECT PlayerName
FROM Players
WHERE Country <> 'USA'
OR Country <> 'Canada' ) p -- Indented qub-suery
ON p.PlayerName = ps.PlayerName
GROUP BY ps.Team
HAVING COUNT(p.PlayerName) >=24;
-- Remove the ORDER BY, it is not required
-- ORDER BY NonNthAmerPlayers DESC
ORDER BY
statementImproving Query Performance in SQL Server