Home | Power BI | Excel | Python | SQL | Visualising Data | Generative AI
This contains notes to accompany the SQL courses.
Power BI, for example, connects to databases and does it all for you
SELECT ReceivedDate,
Region,
Category,
Movement
FROM dbo.MessageView;
SELECT MessageId,
ReceivedDate,
FROM dbo.MessageView
WHERE Movement >= 5;
-- only return large movements
WHERE is flexible; in a list, between, not exists, >and AND and OR clauses
SELECT MessageId,
Movement * 2 AS DoubleMovement
FROM dbo.MessageView;
SELECT MessageId,
ReceivedDate,
Region,
Category,
Movement
FROM dbo.MessageView
ORDER BY Movement;
SELECT ReceivedDate,
SUM(Movement) AS TotalMovement
FROM dbo.MessageView
GROUP BY ReceivedDate,
SELECT
m.MessageId,
c.[Date],
c.DayOfWeek
FROM dbo.MessageView m
INNER JOIN dbo.Calendar c
ON m.ReceivedDate = c.[Date]
+
joins together to form
We need a SQL editor to write and run SQL. Examples are
PatientStay is a small table that we use in many of the exercises.
The Land Registry Tables sho sales of properties in London SW12.
The Chinook database represents an online music store.
Example
SELECT
PatientID
, Hospital
, Ward
, AdmittedDate
FROM PatientStay
WHERE Hospital IN (‘Kingston’, ‘PRUH’)
ORDER BY AdmittedDate DESC;
Syntax
SELECT
<column>
, <column>
, <column
…
FROM <table>
WHERE <where-clause>
ORDER BY <column>;
Example
SELECT
Hospital
, Ward
, COUNT(*) AS NumberOfPatients
, SUM(Tariff) as TotalTariff
FROM PatientStay
WHERE AdmittedDate > ‘2024-03-01’
GROUP BY Hospital, Ward
ORDER BY Hospital, Ward;
Syntax
SELECT <column1>
, <column2>
, AGG(<column>)
…
FROM <table>
WHERE <where-clause>
GROUP BY <column1>, <column2>
ORDER BY <column>, <column2>;
Start with SELECT ps.* FROM PatientStay ps;
Use the autocomplete. It is very helpful.
Take tiny baby steps Start from something that works, not something that doesn’t.
Read the error messages – they are sometimes useful.
Look at where the red squiggerly line starts.
Always put a blank line between statements.
Never have a blank line within a statement.
Click <Ctrl-Return>
anywhere in statement to run
AI is a useful, practical, everyday tool, that we can use now to help us write SQL and learn SQL.
Generative AI large language models (LLMs) such as ChatGPT, Copilot, Claude, Gemini have free editions and are available from a browser.
Pair programmers help us write better SQL from inside the SQL editor e.g. GitHub Copilot inside VSCode editor
Can always revert back to DBeaver
SQL Window functions are extremely useful, for example:
SELECT
ReceivedMonth
, ReceivedDate
, Movement
, ROW_NUMBER() OVER (PARTITION BY ReceivedMonth ORDER BY ReceivedDate) AS RowIndex
, SUM(Movement) OVER (PARTITION BY ReceivedMonth ORDER BY ReceivedDate) AS RunningRollingMovement
FROM
Message;
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
and
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
further limit the rows to be aggregated.