T
Tech Town
Log InSign Up Free
← Back to Blog

June 17, 2025 · admin

SQL Syntax

πŸ“˜ SQL Syntax Explained for Beginners with Examples

Learn the foundational structure of SQL queries to communicate effectively with any database.
πŸ’‘ Master SQL Syntax β€” with real examples, tips, and best practices.


πŸ” What is SQL Syntax?

SQL syntax refers to the set of rules that define how SQL statements must be written and structured to interact with a relational database. Think of SQL as a language, and its syntax as the grammar that ensures your “sentences” (queries) are correct and understandable by the database engine.

No matter which database you use β€” MySQL, PostgreSQL, Oracle, SQLite, or SQL Server β€” the basic SQL syntax remains mostly the same across all platforms.


πŸ“š Basic Structure of an SQL Statement

A typical SQL query follows this structure:

SELECT column1, column2
FROM table_name
WHERE condition
ORDER BY column1 ASC|DESC;

Let’s break down the components one by one.


🧱 Common SQL Clauses and Keywords

βœ… SELECT – Choose the columns you want to retrieve

SELECT first_name, last_name FROM employees;

βœ… FROM – Specify the table to query data from

FROM employees

βœ… WHERE – Filter records using specific conditions

WHERE department = 'Sales'

βœ… ORDER BY – Sort the result set in ascending or descending order

ORDER BY salary DESC

βœ… LIMIT / TOP – Limit the number of rows returned

LIMIT 10 -- MySQL/PostgreSQL
SELECT TOP 10 * FROM employees; -- SQL Server

πŸ’‘ SQL Syntax Rules to Remember

Here are some universal rules for writing clean SQL syntax:

RuleDescription
Case-insensitive keywordsSQL keywords like SELECT, FROM, WHERE are case-insensitive.
Semicolon (;) at endUse a semicolon to terminate a statement, especially when executing multiple queries.
Strings in single quotesUse 'value' for text-based values.
CommentsUse -- for single-line comments and /* */ for multi-line.

Example:

-- This is a single-line comment
SELECT * FROM customers WHERE city = 'London'; /* Fetch customers in London */

πŸ“Œ Sample SQL Query Breakdown

SELECT first_name, last_name, department
FROM employees
WHERE salary > 50000
ORDER BY salary DESC;
  • βœ… SELECT: Retrieves specific columns
  • βœ… FROM: Indicates the table to pull data from
  • βœ… WHERE: Filters employees with salary > 50000
  • βœ… ORDER BY: Sorts the result by salary in descending order

⚠️ Common Mistakes to Avoid

MistakeCorrection
Writing column names in quotesUse quotes only for strings, not column names
Using = instead of LIKE for pattern matchingUse LIKE with % or _
Forgetting the WHERE clause when updating/deletingAlways double-check conditions to avoid deleting all data

πŸ” SQL Syntax Variations (by DBMS)

While the core syntax is the same, some clauses vary:

FeatureMySQLSQL ServerPostgreSQL
Limit rowsLIMITTOPLIMIT
Auto-incrementAUTO_INCREMENTIDENTITYSERIAL

🎯 Practice Challenge

Write an SQL query to fetch the top 5 highest-paid employees from the ’employees’ table.

SELECT first_name, last_name, salary
FROM employees
ORDER BY salary DESC
LIMIT 5;

🏁 Conclusion

Understanding the basic SQL syntax is essential before diving into more complex queries. Think of syntax as the backbone of all SQL operations β€” if your syntax is wrong, your logic won’t even matter.

Practice writing clean and readable queries by following standard syntax rules, and you’ll be querying databases like a pro in no time!