SQL SELECT: Retrieving Data from Your Database


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.


What is the SQL SELECT Statement?

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.

Basic Syntax of SELECT

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

Selecting Specific Columns

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

Using WHERE Clause to Filter Results

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

Using AND, OR, and NOT Operators

You can use logical operators like AND, OR, and NOT in your WHERE clause to combine multiple conditions.

  • AND: Returns records where both conditions are true.
  • OR: Returns records where at least one condition is true.
  • NOT: Excludes records that meet the condition.

Examples:

  1. AND: Retrieve employees with a salary greater than 55,000 and whose last name is "Smith":
    SELECT first_name, last_name, salary 
    FROM employees
    WHERE salary > 55000 AND last_name = 'Smith';
    
  2. OR: Retrieve employees who either earn more than 55,000 or have "Johnson" as their last name:
    SELECT first_name, last_name, salary
    FROM employees
    WHERE salary > 55000 OR last_name = 'Johnson';
    
  3. NOT: Retrieve employees who do not have "Smith" as their last name:
    SELECT first_name, last_name, salary
    FROM employees
    WHERE NOT last_name = 'Smith';
    

Sorting Results with ORDER BY

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

Limiting Results with LIMIT

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

Using DISTINCT to Avoid Duplicates

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