メインコンテンツにスキップ
eLearner.app
モジュール 1 · レッスン 2 / 4コース内の 2/57~10 min
モジュールのレッスン (2/4)

…から選択

The simplest query in SQL is called SELECT and extracts rows from a table. It is the language's "Hello, world", and you will use it in nearly every query you write for the rest of your life. The minimal syntax is:

SQL
SELECT <colonne>
FROM   <tabella>;

Read in English: "take these columns, from the given table". The SQL engine scans the table row by row and for each one returns only the requested columns.

All columns, or just some

  • SELECT * returns all the columns of the table. It is handy for exploring a new dataset, but in "real" code it is avoided: if tomorrow you add a column to the table, the query will silently return more data.
  • SELECT col1, col2 returns only the listed columns, in the order you write them. Changing the order changes the output.
SQL
-- Tutto, comodo per esplorare:
SELECT * FROM employees;

-- Solo le colonne che servono, nell'ordine deciso da te:
SELECT last_name, first_name, salary
FROM employees;

Whitespace, indentation, semicolons

SQL is forgiving with formatting: these three queries are identical to the engine:

SQL
SELECT first_name, last_name FROM employees;

SELECT first_name,last_name FROM employees;

SELECT
  first_name,
  last_name
FROM employees;

The third form is the one you will see most often in professional code: one column per line makes the diff more readable when you add or remove fields.

Try it

運動#sql.m1.l2.e1
試行回数: 0読み込み中…

Extract the first name (first_name) and last name (last_name) of all employees, in that order.

エディターを読み込み中…
ヒントを表示

You need to list the two columns you want between SELECT and FROM, separated by a comma.

3 回の試行後に解決策が利用可能になります

Review exercise

運動#sql.m1.l2.e2
試行回数: 0読み込み中…

Extract email, last_name and salary of the employees (in this exact column order).

エディターを読み込み中…
ヒントを表示

The column order in the result follows the order in which you list them after SELECT.

3 回の試行後に解決策が利用可能になります