Skip to content
Qyubie Learn

Practical Tutorials · Beginner

Your First SQL Queries: SELECT, WHERE, and ORDER BY

A hands-on tutorial on reading data with SQL. Query a real table in your browser, filter rows with WHERE, and sort results with ORDER BY.

  • Qyubie Learn Editorial Team
  • Published
  • 8 min read

Includes 3 exercises and 1 quick check you can do right here.

Almost every app you use — a class list, an online shop, a grading system — keeps its data in a database, and most databases are asked questions in SQL (Structured Query Language). In this tutorial you'll write real queries against a small table of students, right on this page.

By the end you will be able to:

  • read every row and column of a table with SELECT,
  • pick only the columns you need,
  • keep only the rows you want with WHERE, and
  • sort the results with ORDER BY.

What you need: nothing installed. Each example below runs on a fresh SQLite database inside your browser — press Run SQL. You can't break anything; every run starts over.

Step 1: Meet the table

A table is like a spreadsheet: columns describe what's stored (name, course, score) and each row is one record (one student). Here is the table we'll use. The first statements create it and add rows; the last line asks for everything.

Try it · SQL

SELECT * FROM students means "give me every column (*) from the students table." SQL keywords are usually written in capitals so they stand out, but SQLite doesn't mind lowercase.

Step 2: Choose your columns

You rarely need every column. List the ones you want, separated by commas, in the order you want them:

Try it · SQL

Every statement ends with a semicolon ;. It tells the database where one statement stops and the next begins.

Exercise · SQL

Names and courses

The students table is already set up. Write a query that shows only the name and course columns of every student, in that order.

Hint

Start with SELECT, list the two columns with a comma between them, then FROM students;

Show a solution

Try it yourself first — you learn more from a wrong attempt than from reading an answer.

SELECT name, course FROM students;

Step 3: Filter rows with WHERE

WHERE keeps only the rows for which a condition is true. Text values go in single quotes; numbers don't.

Try it · SQL

The comparison operators are the ones you know from math, written for a keyboard:

OperatorMeaningExample
=equal toyear = 1
<> or !=not equal tocourse <> 'BSIT'
> <greater / less thanscore > 80
>= <=at least / at mostscore >= 75

Combine conditions with AND (both must be true) and OR (at least one is true):

Try it · SQL

Quick check

Which students does this query return?
SELECT name FROM students WHERE course = 'BSCS' OR score > 90;

Exercise · SQL

Passing BSIT students

Show the name and score of BSIT students who scored 75 or higher. Keep the table's original order.

Hint

You need two conditions joined with AND: course = 'BSIT' and score >= 75.

Show a solution

Try it yourself first — you learn more from a wrong attempt than from reading an answer.

SELECT name, score FROM students
WHERE course = 'BSIT' AND score >= 75;

Step 4: Sort with ORDER BY

Without ORDER BY, a database may return rows in any order. Add it at the end of the query: ASC sorts smallest first (the default) and DESC largest first.

Try it · SQL

Add LIMIT to keep only the first few rows — handy for a "top 3":

Try it · SQL

The parts of a query always come in this order: SELECT … FROM … WHERE … ORDER BY … LIMIT …. Put WHERE after ORDER BY and you'll get an error.

Exercise · SQL

Second-year leaderboard

List the name and score of second-year students (year 2), highest score first.

Hint

WHERE year = 2 comes first, then ORDER BY score DESC.

Show a solution

Try it yourself first — you learn more from a wrong attempt than from reading an answer.

SELECT name, score FROM students
WHERE year = 2
ORDER BY score DESC;

Common mistakes

  • Double quotes around text. In standard SQL, text values use single quotes: 'BSIT'. Double quotes are for column names.
  • Clauses out of order. It's always SELECT, FROM, WHERE, ORDER BY, LIMIT.
  • Forgetting AND/OR rules. AND narrows the results; OR widens them. When mixing both, add parentheses to make your meaning clear: WHERE course = 'BSIT' AND (year = 1 OR score > 90).
  • Missing semicolons between statements.

Summary

  • SELECT columns FROM table reads data; * means all columns.
  • WHERE keeps rows that match a condition; join conditions with AND and OR.
  • ORDER BY column ASC|DESC sorts; LIMIT n keeps the first n rows.

Next steps: open the playground, choose SQL, and make a table of your own — your subjects and grades, for example — then ask it questions.

References and further reading