SQL - Select Statement
The SELECT statement is one of the most commonly used statements in SQL. It is used to retrieve data from one or more tables in a database. The SELECT statement allows you to specify the columns you want to retrieve, the table(s) from which to retrieve the data, and optional conditions to filter the data.
Columns: Columns are the individual data fields within a table. In the SELECT statement, you specify the columns you want to retrieve by listing their names after the SELECT keyword.
Table: A table is a collection of related data stored in rows and columns. In the SELECT statement, you specify the table(s) from which to retrieve the data by using the FROM keyword followed by the table name(s).
Alias: An alias is an alternative name given to a column or table in the SELECT statement. It provides a shorter or more meaningful name for the column or table, making the query easier to read. Aliases are specified using the AS keyword.
WHERE Clause: The WHERE clause is used to filter the data based on specific conditions. It allows you to specify one or more conditions that must be met for a row to be included in the result set.
ORDER BY Clause: The ORDER BY clause is used to sort the result set based on one or more columns. It allows you to specify the column(s) to sort by and the sort order (ascending or descending).
GROUP BY Clause: The GROUP BY clause is used to group rows based on one or more columns. It is often used in conjunction with aggregate functions (such as SUM, COUNT, AVG) to perform calculations on groups of data.
HAVING Clause: The HAVING clause is used to filter the grouped data based on conditions. It is similar to the WHERE clause but is applied after the GROUP BY clause.
JOIN: Joins are used to combine rows from two or more tables based on related columns between them. Joins allow you to retrieve data from multiple tables in a single SELECT statement.
Aggregate Functions: Aggregate functions are used to perform calculations on a set of values and return a single value. Common aggregate functions include SUM, COUNT, AVG, MIN, and MAX. They are often used with the SELECT statement in combination with the GROUP BY clause.
Subqueries: A subquery is a query nested within another query. It allows you to use the result of one query as input for another query. Subqueries can be used in various parts of a SELECT statement, such as the FROM clause, WHERE clause, or SELECT list.