SQL Basic Syntax

This document provides a comprehensive overview of the fundamental syntax elements used in SQL (Structured Query Language). Understanding these basics is crucial for anyone working with relational databases.

Core SQL Statements

SQL is a declarative language, meaning you tell the database what you want, not necessarily how to get it. The most common SQL statements can be categorized as follows:

Data Query Language (DQL)

Used for retrieving data from the database.

Data Manipulation Language (DML)

Used for managing data within schema objects.

Data Definition Language (DDL)

Used for defining database structure or schema.

Data Control Language (DCL)

Deals with rights, permissions, and other controls on the database system.

Key Syntax Elements

SELECT Statement

The SELECT statement is used to query the database and retrieve a set of records from one or more tables.

Basic SELECT Example:

SELECT column1, column2, ...
FROM table_name
WHERE condition;

INSERT Statement

The INSERT statement is used to add new rows of data into a table.

Basic INSERT Example:

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Note: The order of values must match the order of columns specified. If you are inserting values for all columns, you can omit the column list.

UPDATE Statement

The UPDATE statement is used to modify existing records in a table.

Basic UPDATE Example:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Tip: Always use a WHERE clause with UPDATE. Without it, all rows in the table will be updated.

DELETE Statement

The DELETE statement is used to remove rows from a table.

Basic DELETE Example:

DELETE FROM table_name
WHERE condition;
Tip: Similar to UPDATE, be cautious with the DELETE statement. Without a WHERE clause, all rows will be deleted. To delete all rows without logging the individual row deletions, use TRUNCATE TABLE table_name; (this is a DDL statement).

CREATE TABLE Statement

The CREATE TABLE statement is used to create a new table in the database.

Basic CREATE TABLE Example:

CREATE TABLE table_name (
    column1 datatype constraints,
    column2 datatype constraints,
    column3 datatype constraints,
    ...
);

datatype specifies the type of data a column can hold (e.g., INT, VARCHAR(255), DATE). constraints define rules for the data in a column (e.g., PRIMARY KEY, NOT NULL, UNIQUE, FOREIGN KEY).

ALTER TABLE Statement

The ALTER TABLE statement is used to add, delete, or modify columns in an existing table, or to add or drop constraints.

Examples of ALTER TABLE:

-- Add a new column
ALTER TABLE table_name
ADD column_name datatype;

-- Drop a column
ALTER TABLE table_name
DROP COLUMN column_name;

-- Modify a column's datatype
ALTER TABLE table_name
MODIFY COLUMN column_name new_datatype;

DROP TABLE Statement

The DROP TABLE statement is used to permanently delete a table from the database.

Basic DROP TABLE Example:

DROP TABLE table_name;
Note: This action cannot be undone.

Common SQL Clauses

Data Types

SQL supports a wide range of data types to store different kinds of information. Some common ones include:

Data Type Description
INT / INTEGER Whole numbers.
DECIMAL / NUMERIC Exact numeric values with a fixed precision and scale.
FLOAT / REAL Approximate floating-point numeric values.
VARCHAR(n) Variable-length character strings, up to n characters.
CHAR(n) Fixed-length character strings, n characters long.
TEXT Large character strings.
DATE Stores dates (year, month, day).
TIME Stores time (hour, minute, second).
DATETIME / TIMESTAMP Stores both date and time.
BOOLEAN Stores TRUE or FALSE values.
← Previous: Getting Started Next: Data Types →