SQL Syntax

📝 SQL Syntax refers to the set of rules that define how SQL statements are written and executed. Every SQL command follows a specific structure, allowing the database management system (DBMS) to understand and process the request correctly. Learning SQL syntax is the first step toward writing accurate and efficient database queries.

📖 What is SQL Syntax?

SQL syntax defines the correct arrangement of keywords, table names, column names, operators, and values in an SQL statement. Each SQL statement begins with a command such as SELECT, INSERT, UPDATE, or DELETE, followed by the information required to perform the requested operation.

Information

SQL keywords are traditionally written in UPPERCASE for better readability, although SQL itself is generally case-insensitive for keywords. Table names and column names may be case-sensitive depending on the database system and operating system.

đŸ—ī¸ Basic Structure of an SQL Statement

Most SQL statements follow a predictable pattern. For example, a query that retrieves data typically includes the command, the columns to retrieve, the table name, and optional filtering or sorting clauses.

General SELECT Statement Syntax

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

Remember

Every SQL statement should end with a ; (semicolon). While some tools can execute statements without it, the semicolon is the standard statement terminator and is required when executing multiple SQL statements together.

🔑 Common SQL Keywords

KeywordPurpose
SELECTRetrieves data from one or more tables.
FROMSpecifies the table to query.
WHEREFilters rows based on a condition.
ORDER BYSorts the result set.
GROUP BYGroups rows with similar values.
HAVINGFilters grouped data.
LIMITRestricts the number of returned rows (supported by many databases).

📊 SQL Syntax Examples

1ī¸âƒŖ Retrieve All Columns

Use the * wildcard to retrieve every column from a table.

Select All Columns

SELECT * FROM Students;

2ī¸âƒŖ Retrieve Specific Columns

List only the columns you want to retrieve.

Select Specific Columns

SELECT StudentID, Name
FROM Students;

3ī¸âƒŖ Filter Records

The WHERE clause returns only the rows that satisfy a given condition.

Using WHERE

SELECT *
FROM Students
WHERE Department = 'Computer Science';

4ī¸âƒŖ Sort Results

The ORDER BY clause arranges the result set in ascending or descending order.

Using ORDER BY

SELECT Name, Department
FROM Students
ORDER BY Name ASC;

5ī¸âƒŖ Group Records

The GROUP BY clause combines rows with the same values into groups, often used with aggregate functions.

Using GROUP BY

SELECT Department, COUNT(*) AS TotalStudents
FROM Students
GROUP BY Department;

6ī¸âƒŖ Filter Groups

The HAVING clause filters grouped results after grouping has been performed.

Using HAVING

SELECT Department, COUNT(*) AS TotalStudents
FROM Students
GROUP BY Department
HAVING COUNT(*) > 5;

🧩 SQL Statement Types

Statement TypeExamplePurpose
Data Query SELECTRetrieve data.
Data Definition CREATE, ALTER, DROPCreate or modify database objects.
Data Manipulation INSERT, UPDATE, DELETEAdd, modify, or remove records.
Transaction Control COMMIT, ROLLBACKManage transactions.
Data Control GRANT, REVOKEManage user permissions.

📌 Rules of SQL Syntax

  • ✅ Begin every statement with an SQL command.
  • ✅ Separate multiple column names with commas.
  • ✅ Enclose string values in single quotes ( ' ').
  • ✅ Use numbers without quotes unless required by the data type.
  • ✅ End statements with a semicolon.
  • ✅ Use meaningful table and column names.
  • ✅ Format queries with proper indentation for readability.

âš ī¸ Common Syntax Errors

ErrorCauseSolution
Missing semicolonStatement not properly terminated.Add ; at the end of the statement.
Misspelled keywordIncorrect SQL command.Check the spelling of SQL keywords.
Missing quotesString values are not enclosed in quotes.Use single quotes around text values.
Incorrect table or column nameReferenced object does not exist.Verify table and column names.
Missing commaColumns are not separated correctly.Add commas between column names.

💡 SQL Formatting Best Practices

Best Practice

Write SQL keywords in uppercase, place each clause on a new line, indent nested queries for readability, use descriptive aliases, and avoid using SELECT * in production queries when only specific columns are needed.

📝 Complete Example

The following query demonstrates a well-formatted SQL statement that retrieves student information, filters records, and sorts the results.

Complete SQL Query Example

SELECT StudentID,
       Name,
       Department
FROM Students
WHERE Department = 'Computer Science'
ORDER BY Name ASC;

🚀 Summary

Summary

✅ SQL syntax provides the rules for writing valid SQL statements. By understanding the structure of commands, clauses, keywords, and formatting conventions, you can create readable, maintainable, and efficient SQL queries for any relational database system.