π§Ύ SQL Syntax Guide for Beginners β SELECT, INSERT, UPDATE
π§² Introduction β Why SQL Syntax Matters
Every SQL operationβfrom querying a customer database to creating an entire data warehouseβstarts with correct syntax. SQL syntax defines how statements are written so the database engine can interpret and execute your instructions properly.
Incorrect syntax results in errors and performance issues, while clear and consistent syntax ensures accurate results and maintainable code.
π― In this guide, youβll learn:
- Basic structure of SQL statements
- Syntax for
SELECT
,INSERT
,UPDATE
,DELETE
- Use of semicolons, comments, and indentation
- SQL keywords, case sensitivity, and formatting rules
β 1. General SQL Statement Structure
SQL_KEYWORD column1, column2
FROM table_name
WHERE condition;
β SQL is case-insensitive, but uppercase for keywords improves readability.
π€ 2. SELECT Statement Syntax
SELECT column1, column2
FROM table_name
WHERE condition;
β Retrieves data from one or more columns in a table.
π‘ Tip: Use *
to select all columns.
π 3. INSERT Statement Syntax
INSERT INTO table_name (column1, column2)
VALUES (value1, value2);
β Adds new data to a table.
β οΈ Always match values with column order.
π οΈ 4. UPDATE Statement Syntax
UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;
β Modifies existing records.
β οΈ Use WHERE
to avoid updating all rows.
β 5. DELETE Statement Syntax
DELETE FROM table_name
WHERE condition;
β Removes specific records.
β οΈ Omitting WHERE
deletes all data in the table.
π§Ύ 6. SQL Syntax Rules & Conventions
Rule / Element | Description |
---|---|
π Keywords | Use uppercase for clarity (e.g., SELECT , WHERE ) |
; Semicolon | Terminates statements in SQL (especially in multi-line scripts) |
π¬ Comments | Single-line: -- comment , Multi-line: /* comment */ |
π Whitespace | Ignored by SQL but helps with readability |
π Case Sensitivity | SQL keywords are not case-sensitive, but identifiers may be, depending on the RDBMS |
π Complete List of SQL Syntax (Commands + Clauses)
ποΈ Data Definition Language (DDL) β Define & Modify Structure
Command | Syntax |
---|---|
CREATE TABLE | CREATE TABLE table_name (column1 datatype, column2 datatype); |
ALTER TABLE | ALTER TABLE table_name ADD column_name datatype; |
DROP TABLE | DROP TABLE table_name; |
TRUNCATE TABLE | TRUNCATE TABLE table_name; |
RENAME TABLE | RENAME TABLE old_name TO new_name; |
CREATE DATABASE | CREATE DATABASE db_name; |
DROP DATABASE | DROP DATABASE db_name; |
βοΈ Data Manipulation Language (DML) β Manage Table Data
Command | Syntax |
---|---|
INSERT INTO | INSERT INTO table_name (col1, col2) VALUES (val1, val2); |
UPDATE | UPDATE table_name SET col1 = val1 WHERE condition; |
DELETE | DELETE FROM table_name WHERE condition; |
π Data Query Language (DQL) β Retrieve Data
Command | Syntax |
---|---|
SELECT | SELECT column1, column2 FROM table_name WHERE condition; |
π Data Control Language (DCL) β Permissions & Access
Command | Syntax |
---|---|
GRANT | GRANT SELECT, INSERT ON table_name TO user; |
REVOKE | REVOKE SELECT, INSERT ON table_name FROM user; |
π Transaction Control Language (TCL) β Manage Transactions
Command | Syntax |
---|---|
COMMIT | COMMIT; |
ROLLBACK | ROLLBACK; |
SAVEPOINT | SAVEPOINT savepoint_name; |
RELEASE SAVEPOINT | RELEASE SAVEPOINT savepoint_name; |
SET TRANSACTION | `SET TRANSACTION [READ ONLY |
π Clauses Used in SQL Queries
Clause | Purpose |
---|---|
WHERE | Filters rows based on conditions |
ORDER BY | Sorts results ascending/descending |
GROUP BY | Groups results for aggregation |
HAVING | Filters groups (used with GROUP BY ) |
DISTINCT | Removes duplicate rows |
LIMIT / TOP | Restricts number of returned rows |
OFFSET | Skips rows before returning results |
AS | Creates aliases for columns/tables |
IN / BETWEEN / LIKE | Conditional filters |
IS NULL / IS NOT NULL | Checks for null values |
EXISTS | Checks if subquery returns rows |
UNION / UNION ALL | Combines results from multiple queries |
INTERSECT / EXCEPT | Finds common or exclusive rows |
CASE | Implements if/else logic in queries |
π JOIN Types
Join Type | Syntax |
---|---|
INNER JOIN | SELECT * FROM A INNER JOIN B ON A.id = B.id; |
LEFT JOIN | SELECT * FROM A LEFT JOIN B ON A.id = B.id; |
RIGHT JOIN | SELECT * FROM A RIGHT JOIN B ON A.id = B.id; |
FULL OUTER JOIN | SELECT * FROM A FULL OUTER JOIN B ON A.id = B.id; |
CROSS JOIN | SELECT * FROM A CROSS JOIN B; |
SELF JOIN | SELECT A.name, B.name FROM employees A, employees B WHERE A.manager_id = B.id; |
π Aggregate Functions Syntax
Function | Example |
---|---|
COUNT() | SELECT COUNT(*) FROM employees; |
SUM() | SELECT SUM(salary) FROM employees; |
AVG() | SELECT AVG(age) FROM students; |
MAX() / MIN() | SELECT MAX(price), MIN(price) FROM products; |
π’ Operators in SQL Syntax
Type | Operators |
---|---|
Arithmetic | + , - , * , / , % |
Comparison | = , != , <> , < , > , <= , >= |
Logical | AND , OR , NOT |
Bitwise | & , ` |
π Best Practices
β Do This | β Avoid This |
---|---|
Use consistent formatting | Mixing upper and lowercase randomly |
Comment complex queries | Writing long queries without notes |
Write keywords in uppercase | Lowercasing everything |
Use proper indentation | Writing queries on a single line |
π Summary β Recap & Next Steps
Understanding SQL syntax is the first step in writing clean, error-free queries. Whether you’re fetching, inserting, or modifying data, proper syntax ensures your database runs smoothly.
π Key Takeaways:
- SQL has a simple and readable syntax, based on English
- Each statement ends with a semicolon
- Formatting improves maintainability and teamwork
- Case sensitivity depends on your SQL engine (MySQL, Oracle, PostgreSQL, etc.)
βοΈ Real-World Relevance:
Clean SQL syntax helps avoid bugs, optimize performance, and collaborate effectively in data-driven teams.
β FAQ β SQL Syntax Explained
β Do SQL statements end with a semicolon?
β
Yes, a semicolon ;
is used to terminate statements, especially when running multiple queries in a script.
β Are SQL keywords case-sensitive?
β
No. SELECT
, select
, and SeLeCt
are treated the same. But table/column names may be case-sensitive, depending on your DBMS.
β What happens if I omit the WHERE clause in UPDATE or DELETE?
β οΈ All rows will be updated or deleted. Always double-check before running such queries.
β Can I write SQL in a single line?
β Yes, but it’s not recommended for readability. Break complex statements into multiple lines.
β How do I write comments in SQL?
β
Use --
for single-line or /* ... */
for multi-line comments.
Share Now :