đ 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
đī¸ 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
đ Common SQL Keywords
| Keyword | Purpose |
|---|---|
| SELECT | Retrieves data from one or more tables. |
| FROM | Specifies the table to query. |
| WHERE | Filters rows based on a condition. |
| ORDER BY | Sorts the result set. |
| GROUP BY | Groups rows with similar values. |
| HAVING | Filters grouped data. |
| LIMIT | Restricts 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 Type | Example | Purpose |
|---|---|---|
| Data Query | SELECT | Retrieve data. |
| Data Definition | CREATE, ALTER, DROP | Create or modify database objects. |
| Data Manipulation | INSERT, UPDATE, DELETE | Add, modify, or remove records. |
| Transaction Control | COMMIT, ROLLBACK | Manage transactions. |
| Data Control | GRANT, REVOKE | Manage 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
| Error | Cause | Solution |
|---|---|---|
| Missing semicolon | Statement not properly terminated. | Add ; at the end of the statement. |
| Misspelled keyword | Incorrect SQL command. | Check the spelling of SQL keywords. |
| Missing quotes | String values are not enclosed in quotes. | Use single quotes around text values. |
| Incorrect table or column name | Referenced object does not exist. | Verify table and column names. |
| Missing comma | Columns are not separated correctly. | Add commas between column names. |
đĄ SQL Formatting Best Practices
Best Practice
đ 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;