First Steps Into Practising SQL With Open Data
Made-up tables are great for learning the basics, but real data is more interesting. Many governments and organisations publish open data that anyone can download and use. This guide shows how to turn a public data set into SQL practice.
What open data is
Open data is information published so that anyone can use it, usually under a licence that allows reuse with credit. Examples include public transport timetables, library statistics, weather records and lists of public facilities.
Where to find it
- National open data portals, such as data.gov in the United States and data.gov.uk in the United Kingdom. Many other countries and cities run their own.
- City and regional portals, which often have local data such as parks, bike stations or recycling points.
- Statistics offices, which publish official figures, usually as downloadable tables.
Look for files in CSV format (comma-separated values). They are easy to load into a database.
Step 1: pick a small, friendly data set
Choose something you find interesting and that has a few thousand rows or fewer. A list of public libraries, parks or bus stops works well. Read the description and licence on the portal page.
Step 2: load the CSV into SQLite
The sqlite3 command-line shell can import a CSV directly:
sqlite3 practice.db
sqlite> .mode csv
sqlite> .import parks.csv parks
sqlite> .mode column
sqlite> SELECT COUNT(*) FROM parks;
The first line of the CSV usually becomes the column names. DB Browser for SQLite also has an "Import table from CSV file" option if you prefer a graphical tool.
Step 3: look before you query
SELECT * FROM parks LIMIT 5;
Check the column names, whether numbers look like numbers, and whether some cells are empty. Real data is often messy.
Step 4: ask your own questions
Write a list of questions first, then turn each into SQL. For a parks data set:
| Question | Possible SQL idea |
|---|---|
| How many parks are there? | COUNT(*) |
| Which area has the most parks? | GROUP BY area ORDER BY COUNT(*) DESC |
| Which parks have a playground? | WHERE on a facilities column |
| Which parks have no listed area? | WHERE area IS NULL OR area = '' |
Step 5: clean up a little
You may find extra spaces, inconsistent spellings or numbers stored as text. Practising fixes like TRIM() and UPDATE on your own copy is great experience. Never change the original file; keep it as a backup.
Step 6: credit the source
Most open data licences ask you to say where the data came from. If you share your project, include the portal name, the data set title and the licence in your README.
Things to watch
- Personal data. Open data portals publish data meant to be public, but if you ever find personal details, do not share or publish them.
- Dates. Data sets may be old or updated regularly; note the date you downloaded it.
- Size. Start small. Huge files can be slow on an old laptop.
Why this is worth doing
Answering a real question with your own SQL is one of the most satisfying moments in learning databases, and it makes a great portfolio project. Our beginner course on SELECT queries covers the query skills you will need.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.