SQL Syntax
Understanding SQL syntax is fundamental for writing effective queries and commands in MSSQL Server. SQL statements are composed of keywords, identifiers, variables, constants, and operators.
Key Topics
1. SQL Statements
SQL statements are the instructions used to communicate with the database to perform tasks, queries, and retrieve data.
Example: Basic SQL Statement Structure
SELECT column1, column2 FROM table_name WHERE condition;
Code Explanation: This statement selects column1
and column2
from table_name
where a specific condition is met.
2. Basic Syntax Rules
- SQL statements end with a semicolon (
;
). - SQL keywords are not case-sensitive, but it's a good practice to write them in uppercase.
- Identifiers like table and column names are case-sensitive depending on the database settings.
3. SQL Keywords
SQL uses various keywords to perform different operations. Some common keywords include:
Keyword | Description |
---|---|
SELECT | Retrieves data from the database. |
FROM | Specifies the table to retrieve data from. |
WHERE | Filters records based on a condition. |
INSERT | Inserts new data into a table. |
UPDATE | Modifies existing data in a table. |
DELETE | Removes data from a table. |
JOIN | Combines rows from two or more tables. |
INNER JOIN | Returns records that have matching values in both tables. |
LEFT JOIN | Returns all records from the left table and matched records from the right table. |
RIGHT JOIN | Returns all records from the right table and matched records from the left table. |
FULL JOIN | Returns all records when there is a match in either left or right table. |
GROUP BY | Groups rows that have the same values into summary rows. |
ORDER BY | Sorts the result-set in ascending or descending order. |
HAVING | Filters records that work on summarized GROUP BY results. |
CREATE | Creates a new table, database, index, or view. |
ALTER | Modifies an existing database object. |
DROP | Deletes an entire table, a view of a table or other object in the database. |
TRUNCATE | Removes all records from a table, including all spaces allocated for the records. |
UNION | Combines the result of two or more SELECT statements. |
EXISTS | Checks for the existence of any record in a subquery. |
BETWEEN | Filters the result set within a certain range. |
LIKE | Searches for a specified pattern in a column. |
IN | Specifies multiple possible values for a column. |
AS | Assigns a temporary name to a table or a column for the duration of a query. |
NULL | Represents a missing or undefined value. |
NOT | Negates a condition. |
AND | Combines multiple conditions; all must be true. |
OR | Combines multiple conditions; at least one must be true. |
CASE | Creates conditional logic in queries. |
4. Writing Queries
When writing SQL queries, clarity and readability are important. Proper indentation and formatting help in understanding and maintaining the code.
Example: Well-formatted SQL Query
SELECT
FirstName,
LastName,
BirthDate
FROM
Employees
WHERE
Salary > 50000;
Code Explanation: Selects the first name, last name, and birth date of employees whose salary is greater than 50,000.
Best Practices
- Use uppercase for SQL keywords for better readability.
- Properly format and indent your SQL code.
- Comment your code to explain complex queries.
Key Takeaways
- Understanding SQL syntax is crucial for database operations.
- Proper formatting improves code readability and maintenance.
- Familiarity with SQL keywords is essential for writing queries.