The SELECT
statement is one of the most fundamental commands in SQL. It allows you to retrieve data from one or more tables in a database, making it essential for interacting with data. Whether you're pulling information for a report, building a dynamic website, or just querying a dataset, mastering the SELECT
statement is key to working with SQL effectively.
The SELECT
statement is used to query a database and retrieve information from one or more tables. It specifies the columns you want to retrieve, and can also include filters, sorting, and grouping to tailor the data you get.
A basic SELECT
query might look like this:
SELECT column1, column2
FROM table_name;
column1, column2
: The columns you want to retrieve from the table.table_name
: The name of the table from which you want to retrieve data.Here’s the basic syntax of the SQL SELECT
statement:
SELECT column1, column2, ...
FROM table_name;
If you want to select all columns from a table, you can use the *
wildcard instead of specifying each column individually:
SELECT * FROM table_name;
Example:
Let’s say we have a table called employees
:
id | first_name | last_name | salary |
---|---|---|---|
1 | John | Doe | 50000 |
2 | Jane | Smith | 60000 |
3 | Alice | Johnson | 70000 |
To retrieve all columns from the employees
table:
SELECT * FROM employees;
This will return:
id | first_name | last_name | salary |
---|---|---|---|
1 | John | Doe | 50000 |
2 | Jane | Smith | 60000 |
3 | Alice | Johnson | 70000 |
If you only need specific columns, you can list them in the SELECT
clause:
SELECT first_name, last_name FROM employees;
This will return:
first_name | last_name |
---|---|
John | Doe |
Jane | Smith |
Alice | Johnson |
You can refine your SELECT
queries using the WHERE
clause, which filters the data based on specific conditions.
Example:
Retrieve the employees who earn more than 55,000:
SELECT first_name, last_name, salary
FROM employees
WHERE salary > 55000;
This will return:
first_name | last_name | salary |
---|---|---|
Jane | Smith | 60000 |
Alice | Johnson | 70000 |
You can use logical operators like AND
, OR
, and NOT
in your WHERE
clause to combine multiple conditions.
Examples:
SELECT first_name, last_name, salary
FROM employees
WHERE salary > 55000 AND last_name = 'Smith';
SELECT first_name, last_name, salary
FROM employees
WHERE salary > 55000 OR last_name = 'Johnson';
SELECT first_name, last_name, salary
FROM employees
WHERE NOT last_name = 'Smith';
You can use the ORDER BY
clause to sort the results of your query. By default, it sorts in ascending order (ASC
), but you can specify DESC
for descending order.
Example:
To sort employees by their salary in descending order:
SELECT first_name, last_name, salary
FROM employees
ORDER BY salary DESC;
This will return:
first_name | last_name | salary |
---|---|---|
Alice | Johnson | 70000 |
Jane | Smith | 60000 |
John | Doe | 50000 |
In many cases, you may want to limit the number of records returned. You can use the LIMIT
clause for this.
Example:
Retrieve only the top 2 highest-paid employees:
SELECT first_name, last_name, salary
FROM employees
ORDER BY salary DESC
LIMIT 2;
This will return:
first_name | last_name | salary |
---|---|---|
Alice | Johnson | 70000 |
Jane | Smith | 60000 |
The DISTINCT
keyword is used to return only unique (distinct) values in a column. It removes duplicate values.
Example:
To get a list of unique salaries from the employees
table:
SELECT DISTINCT salary
FROM employees;
This will return:
salary |
---|
50000 |
60000 |
70000 |