The most fundamental operation in SQL is retrieving data from a table. The SELECT
statement is used for this purpose.
To select all columns from a table, you use the SELECT *
syntax:
SELECT *
FROM table_name;
SELECT
: This keyword retrive data from table*
(Asterisk): Indicates that all columns from the table should be retrieved. The asterisk (*
) acts as a wildcard representing all columns in the table.FROM table_name
: Specifies the table from which the data is to be retrieved. Replace table_name
with the actual name of the table you are querying.To select all columns from the actor
table in the Sakila database:
SELECT *
FROM actor;
This query will return all rows and all columns (e.g., actor_id
, first_name
, last_name
, last_update
) from the actor
table.
*
to Select All ColumnsUsing *
to select all columns is generally not recommended. While it may seem convenient, it can lead to several issues:
*
may return unexpected results.Instead of using *
, it is a best practice to explicitly list the columns you need. This approach ensures clarity, reduces the risk of unintended results, and improves query performance.
To retrieve specific columns, list their names in the SELECT
statement, separated by commas:
SELECT column1, column2, column3
FROM table_name;
SELECT column1, column2, column3
: Specifies the columns to retrieve. Replace column1
, column2
, and column3
with the actual column names.FROM table_name
: Indicates the table from which to retrieve the data.To retrieve only the first_name
and last_name
columns from the actor
table:
SELECT first_name, last_name
FROM actor;
This query will return all rows, but only the first_name
and last_name
columns for each actor.
Instead of selecting all columns, you can choose to retrieve only specific columns. To do this, you list the column names you want to retrieve, separated by commas:
SELECT column1, column2, column3
FROM table_name;
SELECT column1, column2, column3
: This specifies the columns you want to retrieve. Replace column1
, column2
, and column3
with the actual names of the columns.FROM table_name
: This specifies the table from which you want to retrieve the data.To select only the first_name
and last_name
columns from the actor
table:
SELECT first_name, last_name
FROM actor;
This query will return all rows, but only the first_name
and last_name
columns for each actor.
The order in which you list columns in the SELECT
statement determines their order in the result set. However, it does not alter the column order in the table itself.
SELECT last_name, first_name
FROM actor;
In this case, the last_name
column will appear before the first_name
column in the output, even though first_name
might be defined earlier in the table structure. The order in the SELECT
statement overrides the default table column order.
Key Takeaways from this Lesson:
SELECT *
retrieves all columns from a table.SELECT column1, column2, ...
retrieves only the specified columns.SELECT
statement determines the order in the result set.