SQL Syntax and Structure
1. SQL Statements
SQL statements are the commands used to interact with a database. They can be used to perform various operations such as querying data, inserting new records, updating existing records, and deleting records.
Example:
SELECT * FROM employees; INSERT INTO employees (name, age) VALUES ('John Doe', 30); UPDATE employees SET age = 31 WHERE name = 'John Doe'; DELETE FROM employees WHERE name = 'John Doe';2. SQL Clauses
SQL clauses are components of SQL statements that perform specific tasks. Common clauses include SELECT
, FROM
, WHERE
, ORDER BY
, and GROUP BY
.
Example:
SELECT name, age FROM employees WHERE department = 'Sales' ORDER BY age DESC;3. SQL Operators
SQL operators are used to perform operations on data within SQL statements. Common operators include arithmetic operators (+
, -
, *
, /
), comparison operators (=
, >
, <
, !=
), and logical operators (AND
, OR
, NOT
).
Example:
SELECT name, salary FROM employees WHERE salary > 50000 AND department = 'Engineering';4. SQL Data Types
SQL data types define the type of data that can be stored in a database column. Common data types include INT
, VARCHAR
, DATE
, FLOAT
, and BOOLEAN
.
Example:
CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(100), age INT, salary FLOAT, hire_date DATE );5. SQL Functions
SQL functions are built-in routines that perform specific tasks. Common functions include aggregate functions (COUNT
, SUM
, AVG
), string functions (CONCAT
, LENGTH
), and date functions (NOW
, DATE_FORMAT
).
Example:
SELECT COUNT(*) AS total_employees FROM employees; SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees; SELECT DATE_FORMAT(hire_date, '%Y-%m-%d') AS formatted_date FROM employees;