CleverSphere

SQL: practising SELECT and WHERE

SELECT picks columns, WHERE the matching rows. Your query runs directly on sample data.

Rules

  • SELECT columns FROM table
  • WHERE filters rows
  • Text in single quotes
  • DISTINCT removes duplicates

Your turn

Three problems on this topic. You can run the query first.

  1. 1 · SELECT & WHERE
  2. 2
  3. 3

List the names of all students from Wien.

students

idnamemajorsemestercity
1GretaBusiness8Berlin
2DavidBusiness8Wien
3AnnaLaw5Berlin
4MaxLaw8Berlin
5NoraBusiness4Bern
6EvaLaw2Basel
7JonasChemistry5Wien
+ More tables: courses, grades

courses

idtitlecreditslecturer
1Statistics5Meier
2Calculus6Weber
3Law3Keller
4Databases3Weber

grades

student_idcourse_idgrade
721.7
222.3
243.3
612.7
541.7
412
622.7
734
142.3
742
644

SQL

Any equivalent query counts. It is also checked on data you can’t see.

Worked example

List the name and city of all Computing students not from Wien.

students

idnamemajorsemestercity
1DavidComputing7Basel
2JonasChemistry7Basel
3ClaraChemistry6Wien
4GretaBusiness2Bern
5FelixChemistry8Bern
6BenChemistry7Bern
7NoraComputing7Wien
+ More tables: courses, grades

courses

idtitlecreditslecturer
1Calculus3Weber
2Marketing3Keller
3Statistics5Keller
4Law6Meier

grades

student_idcourse_idgrade
513.3
724
313
241
621.7
221.7
133.3
642.3
521.7
343
231
412.7
123.7

Worked solution

SELECT name, city FROM students WHERE major = 'Computing' AND city <> 'Wien'

Result on the sample data:

name | city

David | Basel

Typical mistake: Combine conditions with AND or OR.

Common questions

Must my query look exactly like this?

No. The result counts, not how it is written.

Related

More topics