This page demonstrates advanced SQL queries using the DBeaver sample database. Explore window functions, ranking, and aggregation features available in MySQL 8+.

SQL Examples

This page provides practical SQL query examples using the DBeaver sample database. The focus is on advanced SQL features such as window functions, ranking, and aggregation, with results displayed in interactive tables.

The following tables are available in the database:

Table Description
AlbumMusic albums
ArtistMusic artists
CustomerCustomers
EmployeeEmployees
GenreMusic genres
InvoiceInvoices
InvoiceLineInvoice line items
MediaTypeMedia types
PlaylistPlaylists
PlaylistTrackTracks in playlists
TrackMusic tracks
List all artists

SELECT ArtistId, Name FROM Artist LIMIT 10;
ArtistIdName
1AC/DC
2Accept
3Aerosmith
4Alanis Morissette
5Alice In Chains
6Antônio Carlos Jobim
7Apocalyptica
8Audioslave
9BackBeat
10Billy Cobham
List all albums by a specific artist (ArtistId = 1)

SELECT AlbumId, Title FROM Album WHERE ArtistId = 1;
AlbumIdTitle
1For Those About To Rock We Salute You
4Let There Be Rock
List all tracks in a specific genre (GenreId = 1)

SELECT TrackId, Name FROM Track WHERE GenreId = 1 LIMIT 10;
TrackIdName
1For Those About To Rock (We Salute You)
2Balls to the Wall
3Fast As a Shark
4Restless and Wild
5Princess of the Dawn
6Put The Finger On You
7Let's Get It Up
8Inject The Venom
9Snowballed
10Evil Walks
Get total invoice amount per customer

SELECT CustomerId, SUM(Total) as TotalSpent FROM Invoice GROUP BY CustomerId LIMIT 10;
CustomerIdTotalSpent
140
238
340
440
541
650
743
838
938
1038
List employees and their managers

SELECT e.EmployeeId, e.FirstName, e.LastName, m.FirstName AS ManagerFirstName, m.LastName AS ManagerLastName
FROM Employee e
LEFT JOIN Employee m ON e.ReportsTo = m.EmployeeId
LIMIT 10;
EmployeeIdFirstNameLastNameManagerFirstNameManagerLastName
2 Nancy Edwards Andrew Adams
6 Michael Mitchell Andrew Adams
3 Jane Peacock Nancy Edwards
4 Margaret Park Nancy Edwards
5 Steve Johnson Nancy Edwards
7 Robert King Michael Mitchell
8 Laura Callahan Michael Mitchell
1 Andrew Adams
ROW_NUMBER(): Rank tracks by duration within each album

SELECT AlbumId, TrackId, Name, Milliseconds,
       ROW_NUMBER() OVER (PARTITION BY AlbumId ORDER BY Milliseconds DESC) AS TrackRank
FROM Track
WHERE AlbumId IN (1,2)
LIMIT 10;
AlbumIdTrackIdNameMillisecondsTrackRank
1 1 For Those About To Rock (We Salute You) 343719 1
1 14 Spellbound 270863 2
1 10 Evil Walks 263497 3
1 12 Breaking The Rules 263288 4
1 7 Let's Get It Up 233926 5
1 8 Inject The Venom 210834 6
1 13 Night Of The Long Knives 205688 7
1 6 Put The Finger On You 205662 8
1 9 Snowballed 203102 9
1 11 C.O.D. 199836 10
Customers with invoices above average total

SELECT CustomerId, Total, AVG(Total) OVER () AS AvgTotal
FROM Invoice
WHERE Total > (SELECT AVG(Total) FROM Invoice)
LIMIT 10;
CustomerIdTotalAvgTotal
38 6 10.1397
41 6 10.1397
8 6 10.1397
14 6 10.1397
43 6 10.1397
15 6 10.1397
13 6 10.1397
44 6 10.1397
16 6 10.1397
42 6 10.1397
Running total of invoice amounts per customer

SELECT CustomerId, InvoiceId, Total,
       SUM(Total) OVER (PARTITION BY CustomerId ORDER BY InvoiceId) AS RunningTotal
FROM Invoice
ORDER BY CustomerId, InvoiceId
LIMIT 10;
CustomerIdInvoiceIdTotalRunningTotal
1 98 4 4
1 121 4 8
1 143 6 14
1 195 1 15
1 316 2 17
1 327 14 31
1 382 9 40
2 1 2 2
2 12 14 16
2 67 9 25
DENSE_RANK(): Employees by hire date

SELECT EmployeeId, FirstName, LastName, HireDate,
       DENSE_RANK() OVER (ORDER BY HireDate) AS HireRank
FROM Employee
LIMIT 10;
EmployeeIdFirstNameLastNameHireDateHireRank
3 Jane Peacock 2002-04-01 00:00:00 1
2 Nancy Edwards 2002-05-01 00:00:00 2
1 Andrew Adams 2002-08-14 00:00:00 3
4 Margaret Park 2003-05-03 00:00:00 4
5 Steve Johnson 2003-10-17 00:00:00 5
6 Michael Mitchell 2003-10-17 00:00:00 5
7 Robert King 2004-01-02 00:00:00 6
8 Laura Callahan 2004-03-04 00:00:00 7

For more details on SQL window functions and advanced queries, see the MySQL documentation.