Introduction to SQLSQL DatabaseSQL DatatypesSQL QueriesSQL DML StatementsSQL Constraints and IndexingSQL FunctionsStored Procedure and Triggers
1. Basic SELECT Syntax
The basic syntax for a SQL SELECT
statement is as follows:
SELECT column1, column2, ...
FROM table_name;
- SELECT: Specifies the columns to retrieve.
- FROM: Specifies the table from which to retrieve the data.
If you want to select all columns, you can use the *
wildcard:
SELECT * FROM table_name;
2. FROM Clause
The FROM
clause specifies which table(s) to select data from. For example:
SELECT first_name, last_name
FROM employees;
3. WHERE Clause (Filtering Data)
The WHERE
clause filters records based on a given condition:
SELECT first_name, last_name
FROM employees
WHERE age > 30;
You can use operators like =
, !=
, >
, <
, BETWEEN
, IN
, LIKE
, and IS NULL
.
4. ORDER BY (Sorting Data)
The ORDER BY
clause is used to sort the result set. By default, it sorts in ascending order (ASC
), but you can specify descending order with DESC
:
SELECT first_name, last_name
FROM employees
ORDER BY last_name ASC;
5. DISTINCT (Removing Duplicates)
The DISTINCT
keyword removes duplicate rows from the result set:
SELECT DISTINCT department
FROM employees;