Comments and operators play a vital role in enhancing the readability and functionality of SQL code. Comments allow developers to include notes or explanations in their SQL scripts, while operators enable various operations on data. This section discusses the types of comments, operators used in SQL, and their syntax across different database systems.
1. SQL Comments
Comments in SQL are used to explain code, make it more readable, or temporarily disable code without deleting it. There are two types of comments in SQL:
Types of Comments
Comment Type | Syntax Example | Description |
Single-line Comment | — This is a single-line comment | Used to comment a single line. |
Multi-line Comment | /* This is a multi-line comment */ | Used to comment multiple lines. |
Examples:
-- This is a single-line comment
SELECT * FROM Employees; -- Fetch all employees
/* This is a multi-line comment
It can span multiple lines */
SELECT * FROM Departments;
Note: Comments are ignored by the SQL parser and do not affect query execution.
2. Operators
Operators are special symbols used to perform operations on operands (data values). SQL provides various types of operators that can be classified as follows:
a. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations.
Operator | Description | Example |
+ | Addition | SELECT 2 + 3; |
– | Subtraction | SELECT 5 – 2; |
* | Multiplication | SELECT 4 * 3; |
/ | Division | SELECT 8 / 2; |
% | Modulus (remainder) | SELECT 10 % 3; |
Example:
SELECT FirstName, LastName, Salary, Salary * 0.10 AS Bonus
FROM Employees;
b. Comparison Operators
Comparison operators are used to compare two values and return a boolean result (TRUE, FALSE, or UNKNOWN).
Operator | Description | Example |
= | Equal to | SELECT * FROM Employees WHERE Age = 30; |
!= or <> | Not equal to | SELECT * FROM Employees WHERE Age != 30; |
> | Greater than | SELECT * FROM Employees WHERE Salary > 50000; |
< | Less than | SELECT * FROM Employees WHERE Salary < 30000; |
>= | Greater than or equal to | SELECT * FROM Employees WHERE Salary >= 40000; |
<= | Less than or equal to | SELECT * FROM Employees WHERE Salary <= 20000; |
BETWEEN | Within a range | SELECT * FROM Employees WHERE Salary BETWEEN 30000 AND 60000; |
LIKE | Pattern matching | SELECT * FROM Employees WHERE FirstName LIKE ‘A%’; |
Example:
SELECT FirstName, LastName
FROM Employees
WHERE DepartmentID = 3 AND Salary > 60000;
c. Logical Operators
Logical operators are used to combine multiple conditions.
Operator | Description | Example |
AND | Both conditions must be TRUE | SELECT * FROM Employees WHERE Age > 30 AND Salary > 50000; |
OR | At least one condition must be TRUE | SELECT * FROM Employees WHERE Age < 30 OR Salary < 30000; |
NOT | Reverses the result of a condition | SELECT * FROM Employees WHERE NOT (Age > 30); |
Example:
SELECT FirstName, LastName
FROM Employees
WHERE (DepartmentID = 1 OR DepartmentID = 2) AND Salary < 40000;
Conclusion
Understanding comments and operators is essential for writing clean, efficient, and readable SQL code. Comments help clarify code functionality, while operators allow for diverse data manipulations, comparisons, and logical evaluations. Mastery of these elements enhances your ability to interact with databases effectively.
Check out our Trending Courses Demo Playlist
Data Analytics with Power Bi and Fabric |
Could Data Engineer |
Data Analytics With Power Bi Fabic |
AWS Data Engineering with Snowflake |
Azure Data Engineering |
Azure & Fabric for Power bi |
Full Stack Power Bi |
Kick Start Your Career With Our Data Job
Social Media channels
► KSR Datavizon Website :- https://www.datavizon.com
► KSR Datavizon LinkedIn :- https://www.linkedin.com/company/datavizon/
► KSR Datavizon You tube :- https://www.youtube.com/c/
Most Commented