SQL (Structured Query Language) is a programming language designed for managing and manipulating data stored in relational database management systems (RDBMS). SQL is used to perform various operations such as creating, modifying, and querying databases.
There are many SQL databases available, each with their own strengths and weaknesses. Some of the most popular ones include:
SQL syntax is composed of several elements including keywords, identifiers, literals, and symbols. Here are some basic elements:
These are reserved words that have special meaning in SQL. Examples include:
SELECT
FROM
WHERE
CREATE
DROP
These are names given to databases, tables, columns, etc. They must follow certain rules, such as not being a reserved keyword and starting with a letter or underscore.
These are actual values used in SQL statements. Examples include:
1
(numeric literal)'Hello World'
(string literal)'2023-10-01'
(date literal)SQL uses various symbols such as:
*
: Wildcard character to select all columns.;
: Statement terminator.()
: Used to group expressions and define function parameters.""
or ''
: Used to enclose string literals.Here are some basic SQL commands that you should know:
The SELECT
statement is used to retrieve data from one or more tables.
SELECT column1, column2, ...
FROM tablename;
SELECT employee_id, first_name, last_name
FROM employees
WHERE department = 'Sales';
SELECT
: Specifies the columns to retrieve.FROM
: Specifies the table(s) to retrieve data from.WHERE
: Filters records based on conditions.AND
, OR
, NOT
: Used to combine conditions.ORDER BY
: Sorts the result set in ascending or descending order.LIMIT
: Limits the number of rows returned.DISTINCT
: Returns only distinct values.The *
wildcard is used to select all columns.
SELECT *
FROM employees
WHERE salary > 50000;
The INSERT
statement is used to add new records to a database table.
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
INSERT INTO employees (employee_id, first_name, last_name, department)
VALUES (101, 'John', 'Doe', 'Sales');
The UPDATE
statement is used to modify existing records in a database table.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
UPDATE employees
SET department = 'Marketing', salary = 60000
WHERE employee_id = 101;
The DELETE
statement is used to delete records from a database table.
DELETE FROM table_name
WHERE condition;
DELETE FROM employees
WHERE employee_id = 101;
Data manipulation commands are used to modify and manage data in a database.
Used to add new records.
INSERT INTO customers (customer_id, name, email)
VALUES (1, 'John Doe', '[email protected]');
Used to modify existing records.
UPDATE customers
SET email = '[email protected]'