Practise this module for free on a real VM
Sign up for freeSQL Basics
SELECT, WHERE, INSERT, JOIN, GROUP BY. Write 5 SQL statements into a .sql file. Schema is in the task.
Your first SQL queries
SQL basics: your first queries
SQL (Structured Query Language) is the language for querying and changing databases. Data lives in tables (rows = records, columns = fields). Almost every application stores its data in an SQL database.
Your database
Your VM has a real SQLite database ~/schule.db with two tables:
schueler(id,name,klasse) — 6 studentsnoten(id,schueler_id,fach,note) — 12 grades
Have a look: sqlite3 ~/schule.db opens the database, .tables lists the tables, .schema schueler shows the columns, .quit exits. (Right after start the VM is still installing sqlite3 — if the console says "command not found", wait a moment.)
Key commands
SELECT: query data ("show me …").WHERE: filters rows by a condition.INSERT: adds a new record.JOIN: connects two tables via a shared column.GROUP BY+AVG(): groups rows and computes e.g. averages.
You write each query into its own file under /home/student/sql/ and run it with sqlite3 ~/schule.db < ~/sql/file.sql — you immediately see the real result.
How the checker works
It runs your file against a fresh copy of the database and compares the result. So your experiments in ~/schule.db do not get in the way. Broke your database? cp /var/lib/lab/sql/schule.db ~/schule.db restores it.
Your goal
You write five queries — from a simple selection to an analysis across multiple tables.
Exercises
1. Query all students
Concept:
SELECT.SELECT * FROM tablefetches all columns (*) of all rows of a table. Query all students:mkdir -p /home/student/sql echo "SELECT * FROM schueler;" > /home/student/sql/alle.sql sqlite3 ~/schule.db < /home/student/sql/alle.sqlYou see six lines like
1|Anna|7a.Check:
alle.sqlreturns all 6 students with all columns (id,name,klasse). The checker runs your file against the database itself.2. Students of class 7a
Concept:
WHERE. WithWHEREyou filter rows by a condition. Fetch only the students of class7a:echo "SELECT * FROM schueler WHERE klasse = '7a';" > /home/student/sql/klasse-7a.sql sqlite3 ~/schule.db < /home/student/sql/klasse-7a.sqlText values are in single quotes (
'7a').Check:
klasse-7a.sqlreturns exactly the three students of 7a — with all columns.3. Insert a new student
Concept:
INSERT.INSERT INTOadds a new record: first the columns, then the values. The database assigns theiditself. Add the new student Lisa from class 7b and check the result:echo "INSERT INTO schueler (name, klasse) VALUES ('Lisa', '7b');" > /home/student/sql/neu.sql sqlite3 ~/schule.db < /home/student/sql/neu.sql sqlite3 ~/schule.db "SELECT * FROM schueler;"If you run the file twice, Lisa appears twice in your database — no problem: the checker always runs
neu.sqlon a fresh copy.Check: After
neu.sql,schuelercontains the 6 existing students plusLisafrom7b.4. Grades with student name
Concept:
JOIN. AJOINconnects two tables via a shared column —noten.schueler_idholds theidfromschueler. This is how you show grades with the name:echo "SELECT schueler.name, noten.fach, noten.note FROM schueler JOIN noten ON schueler.id = noten.schueler_id;" > /home/student/sql/noten-mit-namen.sql sqlite3 ~/schule.db < /home/student/sql/noten-mit-namen.sqlCheck:
noten-mit-namen.sqlreturns all 12 grades, each withname,fachandnote(in this order).5. Average grade per subject
Concept: aggregation (
AVG+GROUP BY). Aggregate functions combine several rows into one value —AVG()computes the average.GROUP BY fachdoes it per subject:echo "SELECT fach, AVG(note) FROM noten GROUP BY fach;" > /home/student/sql/schnitt.sql sqlite3 ~/schule.db < /home/student/sql/schnitt.sqlCheck:
schnitt.sqlreturns one line per subject with the average grade — forDeutsch,EnglischandMathe.
Knowledge check: SQL basics
No VM, about 3 minutes. You recall what you practised in this module – wrong answers come back once more at the end.
Related courses
Now practice it yourself
Reading is good – doing is better. Practise this module on a real Linux VM, right in your browser. A free account is all it takes.
Start for freeLab content under CC BY 4.0 – free to use with attribution (© TechLogia).
