Setup:
Chinook Database (exported from SQLite and imported CSV files into DuckDB to work with v0.8.0)
DuckDB v0.8.0
PRQL v0.8.0 (via DuckDB Extension)
PRQL-Query v0.0.15
Windows 10
Introduction:
Demonstrating PRQL queries on the Chinook database.
Providing PRQL-Query and resulting SQL queries using the βno-exec option since DuckDB already had the database open. Also used PRQL pipes to pass to pq. Tested the SQL results in DuckDB to confirm results.
Queries:
- Provide a query showing Customers (just their full names, customer ID and country) who are not in the US.
PRQL:
from customers
filter country != 'USA'
select [name = f"{first_name} {last_name}", country, customer_id]
DuckDB results:
βββββββββββββββββββββββββ¬βββββββββββββββββ¬ββββββββββββββ
β name β country β customer_id β
β varchar β varchar β int32 β
βββββββββββββββββββββββββΌβββββββββββββββββΌββββββββββββββ€
β LuΓs GonΓ§alves β Brazil β 1 β
β Leonie KΓΆhler β Germany β 2 β
β FranΓ§ois Tremblay β Canada β 3 β
β Bjorn Hansen β Norway β 4 β
β Frantisek WichterlovΓ‘ β Czech Republic β 5 β
β Helena Holy β Czech Republic β 6 β
β Astrid Gruber β Austria β 7 β
β Daan Peeters β Belgium β 8 β
β Kara Nielsen β Denmark β 9 β
β Eduardo Martins β Brazil β 10 β
β Alexandre Rocha β Brazil β 11 β
β Roberto Almeida β Brazil β 12 β
β Fernanda Ramos β Brazil β 13 β
β Mark Philips β Canada β 14 β
β Jennifer Peterson β Canada β 15 β
β Robert Brown β Canada β 29 β
β Edward Francis β Canada β 30 β
β Martha Silk β Canada β 31 β
β Aaron Mitchell β Canada β 32 β
β Ellie Sullivan β Canada β 33 β
β Β· β Β· β Β· β
β Β· β Β· β Β· β
β Β· β Β· β Β· β
β Dominique Lefebvre β France β 40 β
β Marc Dubois β France β 41 β
β Wyatt Girard β France β 42 β
β Isabelle Mercier β France β 43 β
β Terhi HΓ€mΓ€lΓ€inen β Finland β 44 β
β Ladislav KovΓ‘cs β Hungary β 45 β
β Hugh O'Reilly β Ireland β 46 β
β Lucas Mancini β Italy β 47 β
β Johannes Van der Berg β Netherlands β 48 β
β Stanislaw WΓ³jcik β Poland β 49 β
β Enrique MuΓ±oz β Spain β 50 β
β Joakim Johansson β Sweden β 51 β
β Emma Jones β United Kingdom β 52 β
β Phil Hughes β United Kingdom β 53 β
β Steve Murray β United Kingdom β 54 β
β Mark Taylor β Australia β 55 β
β Diego GutiΓ©rrez β Argentina β 56 β
β Luis Rojas β Chile β 57 β
β Manoj Pareek β India β 58 β
β Puja Srivastava β India β 59 β
βββββββββββββββββββββββββ΄βββββββββββββββββ΄ββββββββββββββ€
β 46 rows (40 shown) 3 columns β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
PRQL-Query: pq --no-exec "from customers | filter country != 'USA' | select [first_name, last_name, country, customer_id]"
result:
SELECT
first_name,
last_name,
country,
customer_id
FROM
customers
WHERE
country <> 'USA'
- Provide a query only showing the Customers from Brazil.
PRQL:
from customers
filter country == 'Brazil'
select [customer_id, name = f"{first_name} {last_name}", country]
DuckDB results:
βββββββββββββββ¬ββββββββββββββββββ¬ββββββββββ
β customer_id β name β country β
β int32 β varchar β varchar β
βββββββββββββββΌββββββββββββββββββΌββββββββββ€
β 1 β LuΓs GonΓ§alves β Brazil β
β 10 β Eduardo Martins β Brazil β
β 11 β Alexandre Rocha β Brazil β
β 12 β Roberto Almeida β Brazil β
β 13 β Fernanda Ramos β Brazil β
βββββββββββββββ΄ββββββββββββββββββ΄ββββββββββ
PRQL-Query: pq --no-exec "from customers | filter country == 'Brazil' | select [customer_id, first_name, last_name, country]"
Notes: had to take out the concatenation of name for pq to work
results:
SELECT
customer_id,
first_name,
last_name,
country
FROM
customers
WHERE
country = 'Brazil'
- Provide a query showing the Invoices of customers who are from Brazil. The resultant table should show the customerβs full name, Invoice ID, Date of the invoice and billing country.
PRQL:
from c=customers
join i=invoices [c.customer_id == i.customer_id]
filter c.country == 'Brazil'
select [c.customer_id, Name = f"{c.first_name} {c.last_name}", i.invoice_id, i.invoice_date, i.billing_country]
take 5
DuckDB results:
βββββββββββββββ¬ββββββββββββββββββ¬βββββββββββββ¬βββββββββββββββ¬ββββββββββββββββββ
β customer_id β Name β invoice_id β invoice_date β billing_country β
β int32 β varchar β int32 β date β varchar β
βββββββββββββββΌββββββββββββββββββΌβββββββββββββΌβββββββββββββββΌββββββββββββββββββ€
β 10 β Eduardo Martins β 25 β 2009-04-09 β Brazil β
β 12 β Roberto Almeida β 34 β 2009-05-23 β Brazil β
β 13 β Fernanda Ramos β 35 β 2009-06-05 β Brazil β
β 11 β Alexandre Rocha β 57 β 2009-09-06 β Brazil β
β 13 β Fernanda Ramos β 58 β 2009-09-07 β Brazil β
βββββββββββββββ΄ββββββββββββββββββ΄βββββββββββββ΄βββββββββββββββ΄ββββββββββββββββββ
PRQL-Query: pq --no-exec "from c=customers | join i=invoices [c.customer_id == i.customer_id] | filter c.country == 'Brazil' | select [c.customer_id, c.first_name, c.last_name, i.invoice_id, i.invoice_date, i.billing_country] | take 5"
results:
SELECT
c.customer_id,
c.first_name,
c.last_name,
i.invoice_id,
i.invoice_date,
i.billing_country
FROM
customers AS c
JOIN invoices AS i ON c.customer_id = i.customer_id
WHERE
c.country = 'Brazil'
LIMIT
5
- Provide a query showing only the Employees who are Sales Agents.
PRQL:
func like fld str -> s"{fld} like '%' || {str} || '%' " # from: https://github.com/PRQL/prql/issues/1123
from employees
filter (like title 'Agent')
select [employee_id, title]
Note: like work around is difficult to currently support two words β had to change from βSales Agentβ to just βAgentβ
DuckDB results:
βββββββββββββββ¬ββββββββββββββββββββββ
β employee_id β title β
β int32 β varchar β
βββββββββββββββΌββββββββββββββββββββββ€
β 3 β Sales Support Agent β
β 4 β Sales Support Agent β
β 5 β Sales Support Agent β
βββββββββββββββ΄ββββββββββββββββββββββ
PRQL-Query: pq --no-exec "func like fld str -> s"{fld} like '%' || {str} || '%' " # from: PRQL/prql#1123 | from employees | filter (like title 'Agent') | select [employee_id, title]"
result: error: unexpected argument βlikeβ found
- Provide a query showing a unique list of billing countries from the Invoice table.
PRQL:
from invoices
select billing_country
group billing_country (take 1)
DuckDB results:
βββββββββββββββββββ
β billing_country β
β varchar β
βββββββββββββββββββ€
β Germany β
β Norway β
β Belgium β
β Canada β
β USA β
β France β
β Ireland β
β United Kingdom β
β Australia β
β Chile β
β India β
β Brazil β
β Portugal β
β Netherlands β
β Spain β
β Sweden β
β Czech Republic β
β Finland β
β Denmark β
β Italy β
β Poland β
β Austria β
β Hungary β
β Argentina β
βββββββββββββββββββ€
β 24 rows β
βββββββββββββββββββ
PRQL-Query: pq --no-exec "from invoices | select billing_country | group billing_country (take 1)"
result:
SELECT
DISTINCT billing_country
FROM
invoices
- Provide a query that shows the invoices associated with each sales agent. The resultant table should include the Sales Agentβs full name.
PRQL:
from c=customers
join side:left i=invoices [i.customer_id == c.customer_id]
join side:left e=employees [e.employee_id == c.support_rep_id]
select [i.invoice_id, employee_name = f"{e.first_name} {e.last_name}"]
DuckDB results:
ββββββββββββββ¬ββββββββββββββββ
β invoice_id β employee_name β
β int32 β varchar β
ββββββββββββββΌββββββββββββββββ€
β 1 β Steve Johnson β
β 2 β Margaret Park β
β 3 β Margaret Park β
β 4 β Steve Johnson β
β 5 β Margaret Park β
β 6 β Jane Peacock β
β 7 β Jane Peacock β
β 8 β Margaret Park β
β 9 β Jane Peacock β
β 10 β Jane Peacock β
β 11 β Jane Peacock β
β 12 β Steve Johnson β
β 13 β Margaret Park β
β 14 β Steve Johnson β
β 15 β Jane Peacock β
β 16 β Steve Johnson β
β 17 β Steve Johnson β
β 18 β Steve Johnson β
β 19 β Margaret Park β
β 20 β Steve Johnson β
β Β· β Β· β
β Β· β Β· β
β Β· β Β· β
β 406 β Steve Johnson β
β 407 β Margaret Park β
β 408 β Steve Johnson β
β 409 β Jane Peacock β
β 410 β Margaret Park β
β 412 β Jane Peacock β
β 41 β Steve Johnson β
β 53 β Jane Peacock β
β 162 β Steve Johnson β
β 173 β Steve Johnson β
β 182 β Jane Peacock β
β 205 β Jane Peacock β
β 227 β Jane Peacock β
β 228 β Steve Johnson β
β 279 β Jane Peacock β
β 357 β Steve Johnson β
β 380 β Steve Johnson β
β 400 β Jane Peacock β
β 402 β Steve Johnson β
β 411 β Jane Peacock β
ββββββββββββββ΄ββββββββββββββββ€
β 412 rows (40 shown) β
ββββββββββββββββββββββββββββββ
PRQL-Query: pq --no-exec "from c=customers | join side:left i=invoices [i.customer_id == c.customer_id] | join side:left e=employees [e.employee_id == c.support_rep_id] | select [i.invoice_id, e.first_name, e.last_name]"
result:
SELECT
i.invoice_id,
e.first_name,
e.last_name
FROM
customers AS c
LEFT JOIN invoices AS i ON i.customer_id = c.customer_id
LEFT JOIN employees AS e ON e.employee_id = c.support_rep_id
- Provide a query that shows the Invoice Total, Customer name, Country and Sale Agent name for all invoices and customers.
PRQL:
from c=customers
join side:left i=invoices [i.customer_id == c.customer_id]
join side:left e=employees [e.employee_id == c.support_rep_id]
select [Customer_Name = f"{c.first_name} {c.last_name}", Employee_Name = f"{e.first_name} {e.last_name}", i.total, c.country, e.title]
DuckDB results:
ββββββββββββββββββββββ¬ββββββββββββββββ¬ββββββββββββββββ¬βββββββββββββββββ¬ββββββββββββββββββββββ
β Customer_Name β Employee_Name β total β country β title β
β varchar β varchar β decimal(10,2) β varchar β varchar β
ββββββββββββββββββββββΌββββββββββββββββΌββββββββββββββββΌβββββββββββββββββΌββββββββββββββββββββββ€
β Leonie KΓΆhler β Steve Johnson β 1.98 β Germany β Sales Support Agent β
β Bjorn Hansen β Margaret Park β 3.96 β Norway β Sales Support Agent β
β Daan Peeters β Margaret Park β 5.94 β Belgium β Sales Support Agent β
β Mark Philips β Steve Johnson β 8.91 β Canada β Sales Support Agent β
β John Gordon β Margaret Park β 13.86 β USA β Sales Support Agent β
β Fynn Zimmermann β Jane Peacock β 0.99 β Germany β Sales Support Agent β
β Niklas SchrΓΆder β Jane Peacock β 1.98 β Germany β Sales Support Agent β
β Dominique Lefebvre β Margaret Park β 1.98 β France β Sales Support Agent β
β Wyatt Girard β Jane Peacock β 3.96 β France β Sales Support Agent β
β Hugh O'Reilly β Jane Peacock β 5.94 β Ireland β Sales Support Agent β
β Emma Jones β Jane Peacock β 8.91 β United Kingdom β Sales Support Agent β
β Leonie KΓΆhler β Steve Johnson β 13.86 β Germany β Sales Support Agent β
β Frank Harris β Margaret Park β 0.99 β USA β Sales Support Agent β
β Jack Smith β Steve Johnson β 1.98 β USA β Sales Support Agent β
β Tim Goyer β Jane Peacock β 1.98 β USA β Sales Support Agent β
β Kathy Chase β Steve Johnson β 3.96 β USA β Sales Support Agent β
β Victor Stevens β Steve Johnson β 5.94 β USA β Sales Support Agent β
β Martha Silk β Steve Johnson β 8.91 β Canada β Sales Support Agent β
β Dominique Lefebvre β Margaret Park β 13.86 β France β Sales Support Agent β
β Steve Murray β Steve Johnson β 0.99 β United Kingdom β Sales Support Agent β
β Β· β Β· β Β· β Β· β Β· β
β Β· β Β· β Β· β Β· β Β· β
β Β· β Β· β Β· β Β· β Β· β
β Kathy Chase β Steve Johnson β 1.98 β USA β Sales Support Agent β
β John Gordon β Margaret Park β 1.98 β USA β Sales Support Agent β
β Victor Stevens β Steve Johnson β 3.96 β USA β Sales Support Agent β
β Robert Brown β Jane Peacock β 5.94 β Canada β Sales Support Agent β
β Madalena Sampaio β Margaret Park β 8.91 β Portugal β Sales Support Agent β
β Manoj Pareek β Jane Peacock β 1.99 β India β Sales Support Agent β
β Enrique MuΓ±oz β Steve Johnson β 0.99 β Spain β Sales Support Agent β
β Terhi HΓ€mΓ€lΓ€inen β Jane Peacock β 8.91 β Finland β Sales Support Agent β
β Enrique MuΓ±oz β Steve Johnson β 1.98 β Spain β Sales Support Agent β
β Enrique MuΓ±oz β Steve Johnson β 13.86 β Spain β Sales Support Agent β
β Terhi HΓ€mΓ€lΓ€inen β Jane Peacock β 1.98 β Finland β Sales Support Agent β
β Terhi HΓ€mΓ€lΓ€inen β Jane Peacock β 7.96 β Finland β Sales Support Agent β
β Terhi HΓ€mΓ€lΓ€inen β Jane Peacock β 5.94 β Finland β Sales Support Agent β
β Enrique MuΓ±oz β Steve Johnson β 8.91 β Spain β Sales Support Agent β
β Terhi HΓ€mΓ€lΓ€inen β Jane Peacock β 0.99 β Finland β Sales Support Agent β
β Enrique MuΓ±oz β Steve Johnson β 1.98 β Spain β Sales Support Agent β
β Enrique MuΓ±oz β Steve Johnson β 3.96 β Spain β Sales Support Agent β
β Terhi HΓ€mΓ€lΓ€inen β Jane Peacock β 1.98 β Finland β Sales Support Agent β
β Enrique MuΓ±oz β Steve Johnson β 5.94 β Spain β Sales Support Agent β
β Terhi HΓ€mΓ€lΓ€inen β Jane Peacock β 13.86 β Finland β Sales Support Agent β
ββββββββββββββββββββββ΄ββββββββββββββββ΄ββββββββββββββββ΄βββββββββββββββββ΄ββββββββββββββββββββββ€
β 412 rows (40 shown) 5 columns β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
PRQL-Query: pq --no-exec "from c=customers | join side:left i=invoices [i.customer_id == c.customer_id] | join side:left e=employees [e.employee_id == c.support_rep_id] | select [c.first_name, c.last_name, e.first_name, e.last_name, i.total, c.country, e.title]"
result:
SELECT
c.first_name AS _expr_0,
c.last_name AS _expr_1,
e.first_name,
e.last_name,
i.total,
c.country,
e.title
FROM
customers AS c
LEFT JOIN invoices AS i ON i.customer_id = c.customer_id
LEFT JOIN employees AS e ON e.employee_id = c.support_rep_id
Note: removed employee name concatenation for pq to provide results
- How many Invoices were there in 2009 and 2011? What are the respective total sales for each of those years?
PRQL:
from invoices
derive year = s"date_part('year', invoice_date)"
group year (
aggregate [count]
)
filter (year == 2009 || year == 2011)
DuckDB results:
βββββββββ¬βββββββββββββββ
β year β count_star() β
β int64 β int64 β
βββββββββΌβββββββββββββββ€
β 2009 β 83 β
β 2011 β 83 β
βββββββββ΄βββββββββββββββ
PRQL-Query: pq --no-exec "from invoices | derive year = s"date_part('year', invoice_date)" | group year (aggregate [count])"
result: error: unexpected argument βinvoice_date) | group year (aggregate [count])β found
- Looking at the InvoiceLine table, provide a query that COUNTs the number of line items for Invoice ID 37.
PRQL:
from invoice_items
filter invoice_id == 37
aggregate [line_items_ID_37 = count]
DuckDB results:
ββββββββββββββββββββ
β line_items_ID_37 β
β int64 β
ββββββββββββββββββββ€
β 4 β
ββββββββββββββββββββ
PRQL-Query: pq --no-exec "from invoice_items | filter invoice_id == 37 | aggregate [line_items_ID_37 = count]"
result:
SELECT
COUNT(*) AS "line_items_ID_37"
FROM
invoice_items
WHERE
invoice_id = 37
- Looking at the Invoices table, provide a query that COUNTs the number of line items for each Invoice.
PRQL:
from invoices
group invoice_id(
aggregate[ct=count]
)
DuckDB results:
ββββββββββββββ¬ββββββββ
β invoice_id β ct β
β int32 β int64 β
ββββββββββββββΌββββββββ€
β 1 β 1 β
β 2 β 1 β
β 3 β 1 β
β 4 β 1 β
β 5 β 1 β
β 6 β 1 β
β 7 β 1 β
β 8 β 1 β
β 9 β 1 β
β 10 β 1 β
β 11 β 1 β
β 12 β 1 β
β 13 β 1 β
β 14 β 1 β
β 15 β 1 β
β 16 β 1 β
β 17 β 1 β
β 18 β 1 β
β 19 β 1 β
β 20 β 1 β
β Β· β Β· β
β Β· β Β· β
β Β· β Β· β
β 393 β 1 β
β 394 β 1 β
β 395 β 1 β
β 396 β 1 β
β 397 β 1 β
β 398 β 1 β
β 399 β 1 β
β 400 β 1 β
β 401 β 1 β
β 402 β 1 β
β 403 β 1 β
β 404 β 1 β
β 405 β 1 β
β 406 β 1 β
β 407 β 1 β
β 408 β 1 β
β 409 β 1 β
β 410 β 1 β
β 411 β 1 β
β 412 β 1 β
ββββββββββββββ΄ββββββββ€
β 412 rows β
β (40 shown) β
ββββββββββββββββββββββ
PRQL-Query: pq --no-exec "from invoices | group invoice_id(aggregate[ct=count])"
result:
SELECT
invoice_id,
COUNT(*) AS ct
FROM
invoices
GROUP BY
invoice_id
Credits:
Queries adapted from from Alaa Sedeeq
Source: https://www.kaggle.com/code/alaasedeeq/chinook-questions-with-sqlite/notebook under the Apache 2.0 open source license.