Computer Science FundamentalsLesson 3 of 3 · tap to see all lessons
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:
| Operator | Meaning | Example |
|---|---|---|
= | equal to | year = 1 |
<> or != | not equal to | course <> 'BSIT' |
> < | greater / less than | score > 80 |
>= <= | at least / at most | score >= 75 |
Combine conditions with AND (both must be true) and OR (at least one is true):
Try it · SQL
Quick check
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.
ANDnarrows the results;ORwidens 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 tablereads data;*means all columns.WHEREkeeps rows that match a condition; join conditions withANDandOR.ORDER BY column ASC|DESCsorts;LIMIT nkeeps 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
Keep learning
- ProgrammingBeginner
Java Arrays and Methods: 1D and 2D Arrays, Searching, Parameters, and Return Values
Store many values in Java arrays, loop through 1D and 2D arrays, search them, and organize code into methods with parameters, return values, and overloading.
10 min read2 practice itemsUpdated December 5, 2026 - ProgrammingBeginner
Database Basics for Java Developers: Tables, Keys, MySQL Data Types, and Workbench
Learn why applications store data in a database, how tables, rows, and columns work, which MySQL data types to choose, and how primary and foreign keys link tables.
12 min read4 practice itemsUpdated December 5, 2026 - ProgrammingBeginner
Java Program Structure and Syntax: Your First Program Line by Line
Understand every line of a first Java program: classes, main, statements, comments, identifiers, keywords, and the three kinds of errors.
8 min read4 practice itemsUpdated December 5, 2026