Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 125 ยท Getting Started

Your First SQL Queries

Learn what SQL is and write your first queries against a real SQLite database in your browser. Select columns, filter rows with WHERE and sort with ORDER BY.

Almost every app you use keeps its data in a database: shops store products and orders, banks store transactions, and this website stores its lessons. SQL (Structured Query Language, often pronounced "sequel") is the language used to ask those databases questions.

Every example in this lesson runs against a real SQLite database inside your browser. The database belongs to a small bookstore and has four tables: authors, books, customers and orders. Experiment freely: each run starts from a fresh copy, so you can't break anything.

Tables, rows and columns

A database table looks a lot like a spreadsheet:

  • Each column has a name and holds one kind of information, such as title or price.
  • Each row is one record, such as one book.

To see everything in a table, use SELECT *. The asterisk means "all columns":

SQL All books ยท database: bookstore

Press Run (or Ctrl + Enter) to see the result here.

Read the query almost like an English sentence: select everything from books. The semicolon marks the end of the statement.

Choosing columns

Most of the time you only need a few columns. List them after SELECT, separated by commas:

SQL Selected columns ยท database: bookstore

Press Run (or Ctrl + Enter) to see the result here.

SQL keywords like SELECT and FROM are not case-sensitive, but writing them in capitals is a common convention that makes queries easier to scan. Line breaks don't matter either, so long queries are usually spread across several lines.

Filtering rows with WHERE

WHERE keeps only the rows that match a condition:

SQL Filtering ยท database: bookstore

Press Run (or Ctrl + Enter) to see the result here.

Text values go in single quotes. Numbers don't need quotes, and you can compare them with <, >, <=, >=, = and <> (not equal):

SQL Comparing numbers ยท database: bookstore

Press Run (or Ctrl + Enter) to see the result here.

AND requires both conditions to be true, while OR needs only one. Try changing the query to find books that are either out of stock or cost more than 20.

Sorting with ORDER BY

Rows come back in no guaranteed order unless you ask for one. ORDER BY sorts them, smallest first by default; add DESC for descending order:

SQL Sorting ยท database: bookstore

Press Run (or Ctrl + Enter) to see the result here.

LIMIT 5 keeps only the first five rows, which here gives the five newest books.

Missing values: NULL

Sometimes a value is simply unknown. SQL represents that with NULL, which means "no value", not zero and not empty text. One author in our database has no recorded birth year:

SQL Finding NULL ยท database: bookstore

Press Run (or Ctrl + Enter) to see the result here.

= NULL never matches. Because NULL means "unknown", comparing anything to it with = gives an unknown result rather than true. Always use IS NULL or IS NOT NULL.

What SQL is, and what it is not

SQL is declarative: you describe the rows you want and the database decides how to find them. A SQL system has a few cooperating parts: a parser that checks your text, an optimizer that picks an execution plan, an execution engine that reads and writes the stored data, and a storage layer that keeps tables, indexes and transaction logs safe on disk. Statements fall into groups: SELECT queries (DQL), INSERT/UPDATE/DELETE (DML), CREATE/ALTER/DROP (DDL), GRANT/REVOKE (DCL) and COMMIT/ROLLBACK (TCL). The command types lesson goes through each group.

SQL is not the best tool for everything. Its limits are worth knowing from the start:

  • It isn't a general programming language. Plain SQL has no loops or variables; each database adds its own procedural extension (PL/pgSQL, T-SQL, PL/SQL) for that.
  • Dialects differ. The core (SELECT, JOIN, GROUP BY) is portable, but date functions, string functions, auto-numbering and row limits are spelled differently in every database. The dialects lesson maps the differences.
  • Tables fit tabular data best. Deeply nested documents, graphs and huge unstructured files are awkward (see SQL and NoSQL).
  • Scaling takes work. One database server handles an enormous amount, but spreading writes over many servers needs design effort (replication and scaling).
  • Bad queries are easy to write. Because the language hides the execution, a query that looks harmless can scan millions of rows. Learning to read plans is part of the craft.

Practice

Write a query that lists the names and cities of all customers who joined in 2024, sorted by name. Dates here are stored as text like '2024-03-19', so joined >= '2024-01-01' works as a filter.

SQL Your turn ยท database: bookstore

Press Run (or Ctrl + Enter) to see the result here.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which query returns only the title and price columns of every book?

    List the columns you want after SELECT, then name the table after FROM. * means every column.
  2. 2. How do you find authors whose birth year is unknown?

    NULL means unknown, so comparing with = never matches. Use IS NULL.
  3. 3. What does ORDER BY price DESC LIMIT 3 return?

    DESC sorts from highest to lowest, and LIMIT 3 keeps the first three of that order.
  4. 4. Which condition matches books that are cheaper than 15 AND in stock?

    AND requires both conditions to be true; OR would also match expensive books that are in stock.

Summary

  • A database stores data in tables made of rows and columns.
  • SELECT columns FROM table reads data; * means every column.
  • WHERE filters rows; combine conditions with AND and OR.
  • ORDER BY column [DESC] sorts the results and LIMIT n keeps the first n rows.
  • NULL means "no value"; test for it with IS NULL.

Next: Databases, Tables and Keys, where you'll look at how relational databases are organized.