Skip to main content

SQL SELECT Statement

SQL SELECT Statement

The SQL SELECT statement is used to retrieve data from one or more tables in a database.


1. Basic Syntax

The basic syntax of the SELECT statement is:

SELECT column1, column2, ...
FROM table_name;

Example:

// Example of selecting all columns from the "employees" table
SELECT *
FROM employees;

This example retrieves all columns from the "employees" table.


2. Filtering Data

You can filter the retrieved data using the WHERE clause:

// Example of selecting data from the "employees" table where the age is greater than 30
SELECT *
FROM employees
WHERE age > 30;

This example retrieves data from the "employees" table where the age column is greater than 30.


3. Sorting Data

You can sort the retrieved data using the ORDER BY clause:

// Example of selecting data from the "employees" table sorted by the "last_name" column in descending order
SELECT *
FROM employees
ORDER BY last_name DESC;

This example retrieves data from the "employees" table and sorts it by the "last_name" column in descending order.


4. Limiting Results

You can limit the number of rows returned using the LIMIT clause:

// Example of selecting the first 10 rows from the "employees" table
SELECT *
FROM employees
LIMIT 10;

This example retrieves the first 10 rows from the "employees" table.


5. Conclusion

The SELECT statement is essential in SQL for retrieving data from tables in a database. By combining it with clauses such as WHERE, ORDER BY, and LIMIT, developers can filter, sort, and limit the results to suit their needs.

Comments