Select All Ctrl-Enter: Plain Query Results, Alt-T: Rich Query Results, Alt-Enter: Line Chart Results, Alt-I: Pivot Chart Results, Ctrl-Space or Tab: Autocomplete.
xxxxxxxxxx
1
2
SELECT *
3
FROM Artist
4
Select a maximum of 10 records
Ctrl-Enter: Plain Query Results, Alt-T: Rich Query Results, Alt-Enter: Line Chart Results, Alt-I: Pivot Chart Results, Ctrl-Space or Tab: Autocomplete.
xxxxxxxxxx
1
2
SELECT *
3
FROM Artist
4
LIMIT 10
5
Aggregate using Sum
Ctrl-Enter: Plain Query Results, Alt-T: Rich Query Results, Alt-Enter: Line Chart Results, Alt-I: Pivot Chart Results, Ctrl-Space or Tab: Autocomplete.
xxxxxxxxxx
1
2
SELECT i.billingcountry, sum(total) as 'TotalSales'
3
FROM invoice AS i
4
GROUP BY billingcountry
5
ORDER BY totalsales DESC
6
Filter an Aggregate Value
Ctrl-Enter: Plain Query Results, Alt-T: Rich Query Results, Alt-Enter: Line Chart Results, Alt-I: Pivot Chart Results, Ctrl-Space or Tab: Autocomplete.
xxxxxxxxxx
1
2
-- Filter an aggregate results using the 'HAVING' clause.
3
SELECT BillingCountry, sum(Total) as TotalSales
4
FROM Invoice AS i
5
GROUP BY BillingCountry
6
HAVING TotalSales > 100
7
ORDER BY totalsales DESC
8
9
Get Max
Ctrl-Enter: Plain Query Results, Alt-T: Rich Query Results, Alt-Enter: Line Chart Results, Alt-I: Pivot Chart Results, Ctrl-Space or Tab: Autocomplete.
xxxxxxxxxx
1
2
SELECT i.billingcountry, max(total) as 'Largest Sale'
3
FROM invoice AS i
4
GROUP BY billingcountry
5
ORDER BY 'Largest Sale' DESC
6
Join Two Tables - Where the Same ID is Present in Both
Ctrl-Enter: Plain Query Results, Alt-T: Rich Query Results, Alt-Enter: Line Chart Results, Alt-I: Pivot Chart Results, Ctrl-Space or Tab: Autocomplete.
xxxxxxxxxx
1
2
SELECT Name, Title
3
FROM Artist AS A
4
JOIN Album as I
5
ON A.ArtistID = I.ArtistID
6
Join Two Tables - Taking All Entries from First Table and Any Matching Entries from Second Table
Ctrl-Enter: Plain Query Results, Alt-T: Rich Query Results, Alt-Enter: Line Chart Results, Alt-I: Pivot Chart Results, Ctrl-Space or Tab: Autocomplete.
xxxxxxxxxx
1
2
SELECT Name, Title
3
FROM Artist AS A
4
LEFT JOIN Album as I
5
ON A.ArtistID = I.ArtistID
6
Find ID in One Table But Not the Other
Ctrl-Enter: Plain Query Results, Alt-T: Rich Query Results, Alt-Enter: Line Chart Results, Alt-I: Pivot Chart Results, Ctrl-Space or Tab: Autocomplete.
xxxxxxxxxx
1
2
SELECT DISTINCT Name
3
FROM Artist AS A
4
LEFT JOIN Album as I
5
ON A.ArtistID = I.ArtistID
6
WHERE
7
I.ArtistID IS NULL
8
Get the Entry with the Most Recent Date for an Entity
Ctrl-Enter: Plain Query Results, Alt-T: Rich Query Results, Alt-Enter: Line Chart Results, Alt-I: Pivot Chart Results, Ctrl-Space or Tab: Autocomplete.
xxxxxxxxxx
1
2
-- Join a table to itself and select the latest date per CustomerID.
3
SELECT I1.*
4
FROM Invoice I1
5
LEFT JOIN Invoice I2
6
ON
7
(
8
I1.CustomerID = I2.CustomerID
9
AND
10
I1.InvoiceDate < I2.InvoiceDate
11
)
12
WHERE
13
I2.CustomerID IS NULL
14
Pull in a Value from the Previous Row of the Results
Ctrl-Enter: Plain Query Results, Alt-T: Rich Query Results, Alt-Enter: Line Chart Results, Alt-I: Pivot Chart Results, Ctrl-Space or Tab: Autocomplete.
xxxxxxxxxx
1
2
-- Order by Customer ID and Invoice Date and pull in the invoice date from the
3
-- previous row.
4
SELECT CustomerID
5
, InvoiceDate
6
, MIN(InvoiceDate) OVER (PARTITION BY CustomerID ORDER BY CustomerID, InvoiceDate
7
ROWS BETWEEN 1 PRECEDING and 1 PRECEDING) as PreviousInvoiceDate
8
FROM Invoice
9
ORDER BY CustomerID, InvoiceDate
Get the Number of Days Between Two Dates
Ctrl-Enter: Plain Query Results, Alt-T: Rich Query Results, Alt-Enter: Line Chart Results, Alt-I: Pivot Chart Results, Ctrl-Space or Tab: Autocomplete.
xxxxxxxxxx
1
2
-- In the subquery we order by Customer ID and Invoice Date and pull in the invoice date from the
3
-- previous row. Then we count the instances for each DaysBetweenInvoice.
4
SELECT
5
(InvoiceDate - PreviousInvoiceDate) DaysBetweenInvoice
6
, COUNT(*) Invoices
7
FROM
8
(
9
SELECT CustomerID
10
, InvoiceDate
11
, MIN(InvoiceDate) OVER (PARTITION BY CustomerID ORDER BY CustomerID, InvoiceDate
12
ROWS BETWEEN 1 PRECEDING and 1 PRECEDING) as PreviousInvoiceDate
13
FROM Invoice
14
ORDER BY CustomerID, InvoiceDate
15
) A
16
WHERE
17
PreviousInvoiceDate IS NOT NULL
18
GROUP BY 1
19