Select Statement in SQL
SELECT Statement in SQL
The SELECT statement is the most commonly used SQL command to retrieve data from one or more tables in a database.
Basic Syntax:
SELECT column1, column2, ...FROM table_nameWHERE conditionORDER BY column1 [ASC|DESC]LIMIT number;Explanation of parts:
SELECT column1, column2, ...: Specifies which columns to retrieve.FROM table_name: Specifies the table to query.WHERE condition: Filters the rows returned.ORDER BY column1 [ASC|DESC]: Sorts the results.LIMIT number: Limits the number of rows returned (syntax varies by DB).
Examples:
Select all columns from a table:
SELECT * FROM employees;Select specific columns:
SELECT name, salary FROM employees;Select with a condition:
SELECT name, salary FROM employees WHERE department = 'Sales';Select with sorting:
SELECT name, salary FROM employees ORDER BY salary DESC;Select with limit (MySQL/PostgreSQL):
SELECT name FROM employees ORDER BY hire_date DESC LIMIT 5;Notes:
You can use aggregate functions (
SUM(),AVG(),MAX(), etc.) withSELECT.You can join multiple tables with
JOINinsideFROM.The
SELECTstatement returns a result set (rows and columns).
If you want examples of complex SELECT queries or specific use cases, just ask!