SQL and MySQL course Β· Module 1: Introduction to databases

Rules for writing queries

2 min read Β·
In this lesson4

The Library's language has a few simple customs. Stick to them and your queries will always be readable.

The semicolon ends an order

We end every SQL command with a semicolon ";". It is a signal to the Library: "this is the whole order, you may run it".

SELECT * FROM autorzy;

The semicolon comes once, at the very end of the command - not after every column or after the shelf name.

Letter case

SQL keywords (SELECT, FROM, WHERE...) can be written in lower or upper case - it makes no difference to the Library. By convention, however, we write them in UPPERCASE to distinguish them from column and table names:

SELECT imie, nazwisko FROM czytelnicy;

Comments

Sometimes we want to leave a note in the margin of a scroll. A single-line comment starts with two dashes --:

-- This query shows all authors
SELECT * FROM autorzy;

Leave a space after the two dashes - MySQL needs it to recognize a comment. MySQL also understands comments that start with #, but in this land we stick to the standard --. The Library ignores comments - they are only for us, the scribes.

Whitespace

SQL does not care about spaces and new lines. These two queries are identical:

SELECT tytul FROM ksiazki;
SELECT tytul
FROM ksiazki;

We usually split longer queries across lines - it is more readable that way. Remember: the semicolon ends an order, UPPERCASE marks the keywords, and a -- comment is a note in the margin - every good scribe keeps these customs.

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. Which character ends an SQL query?

  2. 2. How does a single-line comment begin in SQL?

  3. 3. How does SQL treat the case of keywords (e.g. SELECT vs select)?

  4. 4. Are these two queries equivalent: "SELECT tytul FROM ksiazki;" and the same one split across two lines?

Hands-on tasks in the game

  • Click in order

    Click the elements in the order in which we build a query that retrieves title and author (autor_id):

  • Code editor

    Write a query that retrieves the columns imie and nazwisko from the czytelnicy table.

  • Horizontal ordering

    Arrange on one line a query that retrieves readers' first and last names:

  • Code editor

    Write a query that retrieves all columns and all rows from the czytelnicy table.

Useful articles