SQL

SQL - Select

SQL (Structured Query Language) is used to communicate with databases. The SELECT statement is one of the most commonly used SQL commands. It is used to fetch data from a database. The basic syntax of a SELECT query is:

Basic Syntax

SELECT column1, column2, ...
FROM table_name
[WHERE condition]
[GROUP BY column1, column2, ...]
[HAVING condition]
[ORDER BY column1, column2, ... [ASC|DESC]]
[LIMIT number];

Here's a breakdown of the components:

  • SELECT: Specifies the columns to be retrieved.
  • column1, column2, ...:The names of the columns to retrieve data from. Use * to select all columns.
  • FROM table_name: Specifies the table from which to retrieve the data.
  • WHERE condition: (Optional) Filters the records to return only those that meet a specific condition.
  • GROUP BY column1, column2, ...: (Optional) Groups rows that have the same values in specified columns into aggregated data.
  • HAVING condition: (Optional) Filters the groups created by the GROUP BY clause based on a condition.
  • ORDER BY column1, column2, ... [ASC|DESC]: (Optional) Sorts the result set by specified columns in ascending (ASC) or descending (DESC) order.
  • LIMIT number: (Optional) Limits the number of rows returned.