SQL Syntax and Statements Overview | Learn Basic SQL Commands
Introduction
Structured Query Language (SQL) is the standard language used to
interact with relational databases. It allows users to create, read,
update, and delete data — commonly known as CRUD operations. SQL
uses a simple, human-readable syntax that is easy to learn but
extremely powerful in practice.
In this section, you'll learn the basic SQL syntax and explore the
most commonly used SQL statements
Basic SQL Syntax Rules
Before diving into individual commands, it’s important to understand the general structure and rules of SQL syntax
- SQL keywords are not case-sensitive, but it’s common to write them in uppercase for clarity.
- Statements usually end with a semicolon (
;
). -
SQL follows a structured format:
COMMAND table_name
followed by specific clauses. -
Strings are enclosed in single quotes (
'example'
). -
Column and table names should not have spaces (use underscores
_
if needed).
Example:
SELECT first_name, last_name FROM customers WHERE city = 'New York';
Overview of Core SQL Statements
1 SELECT – Retrieve Data from a Table Used to fetch data from one or more tables.
CSELECT column1, column2 FROM table_name;
Example:
SELECT name, age FROM students;
2. INSERT INTO – Add New Data Adds new records to a table.
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
Example:
INSERT INTO employees (name, position) VALUES ('Alice', 'Manager');
3. UPDATE – Modify Existing Data Changes existing records in a table.
UPDATE table_name SET column1 = value1 WHERE condition;
Example:
UPDATE products SET price = 19.99 WHERE product_id = 5;
4. DELETE – Remove Data from a Table Deletes records from a table.
DELETE FROM table_name WHERE condition;
Example:
DELETE FROM orders WHERE order_id = 10;
SQL Table Management Statements
5. CREATE TABLE – Create a New Table Defines a new table and its columns.
CCREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
);
Example:
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100)
);
6. ALTER TABLE – Modify Table Structure Used to add, delete, or modify columns in an existing table.
CALTER TABLE table_name ADD column_name datatype;
Example:
ALTER TABLE customers ADD phone_number VARCHAR(15);
7. DROP TABLE – Delete a Table Removes a table and all its data permanently.
DROP TABLE table_name;
Example:
DROP TABLE temp_data;
Filtering and Sorting Data
WHERE – Filter rows based on conditions
SELECT * FROM employees WHERE department = 'Sales';
ORDER BY – Sort results by columns
SELECT * FROM products ORDER BY price ASC;
LIMIT – Restrict the number of rows returned
SELECT * FROM students LIMIT 5;
Joins and Relationships (Preview)
SQL allows combining data from multiple tables using JOINs. Examples include:
- INNER JOIN
- LEFT JOIN
- RIGHT JOIN
- FULL OUTER JOIN
These will be covered in-depth in the "Working with Multiple Tables" module.
Conclusion
SQL is a powerful language with a logical and readable syntax. By mastering basic SQL statements such as SELECT, INSERT, UPDATE, DELETE, and table creation commands like CREATE TABLE, you build a strong foundation for managing data in any relational database.