跳转到主要内容
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 次尝试后可用的解决方案