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

SELECT in practice

2 min read Β·
In this lesson4

You already know SELECT, FROM and the semicolon. Now we will practice combining them across different shelves. Everything you need you have already learned - here it is about fluency.

A single column

To take just one field, give its name after the SELECT keyword:

SELECT nazwa FROM kategorie;

The Library will return a list of section names only - without the identifiers or other fields.

Several columns

We list columns separated by commas. The order in which you give them is the order in which they appear in the result:

SELECT imie, nazwisko, email FROM czytelnicy;

This returns three fields for each reader - first name, last name and e-mail, in exactly that order.

The primary key is a column too

id is an ordinary column - you can retrieve it just like any other:

SELECT id, tytul FROM ksiazki;

The result shows the catalog number and the title of every book - in exactly the order in which you listed the columns.

Different shelves, the same syntax

The pattern is always the same: SELECT columns FROM table;. Only the shelf name and the list of fields change:

SELECT data_wypozyczenia FROM wypozyczenia;

You get only the borrowing dates - nothing about who took which book, because you did not ask for those fields.

In the same way you take fields from the authors shelf - this time three of them, separated by commas:

SELECT imie, nazwisko, kraj FROM autorzy;

The more queries you write, the more this rhythm will become second nature. It is the foundation of the entire journey ahead!

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. How many columns will the query "SELECT tytul, cena FROM ksiazki;" return?

  2. 2. What do we separate column names with in a SELECT query?

  3. 3. What does the SELECT command do?

Hands-on tasks in the game

  • Code editor

    Write a query that retrieves the columns tytul and cena from the ksiazki table.

  • Horizontal ordering

    Arrange on one line a query that retrieves category names:

  • Code editor

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

  • Code editor

    Write a query that retrieves the columns imie, nazwisko and kraj from the autorzy table.

  • Horizontal ordering

    Arrange on one line a query that retrieves readers' e-mail addresses (left to right):

  • Code editor

    Write a query that retrieves only the data_wypozyczenia column from the wypozyczenia table.

  • Click in order

    Click the elements in order to build a query that retrieves the books' years of publication:

  • Code editor

    Write a query that retrieves the columns tytul and rok_wydania from the ksiazki table.

  • Horizontal ordering

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

  • Horizontal ordering

    Arrange on one line a query that retrieves the authors' countries (left to right):

  • Code editor

    Write a query that retrieves the columns id and tytul from the ksiazki table.

Useful articles