Comments and Operators in SQL

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 TypeSyntax ExampleDescription
Single-line Comment— This is a single-line commentUsed 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.

OperatorDescriptionExample
+AdditionSELECT 2 + 3;
SubtractionSELECT 5 – 2;
*MultiplicationSELECT 4 * 3;
/DivisionSELECT 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).

OperatorDescriptionExample
=Equal toSELECT * FROM Employees WHERE Age = 30;
!= or <>Not equal toSELECT * FROM Employees WHERE Age != 30;
Greater thanSELECT * FROM Employees WHERE Salary > 50000;
Less thanSELECT * FROM Employees WHERE Salary < 30000;
>=Greater than or equal toSELECT * FROM Employees WHERE Salary >= 40000;
<=Less than or equal toSELECT * FROM Employees WHERE Salary <= 20000;
BETWEENWithin a rangeSELECT * FROM Employees WHERE Salary BETWEEN 30000 AND 60000;
LIKEPattern matchingSELECT * 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.

OperatorDescriptionExample
ANDBoth conditions must be TRUESELECT * FROM Employees WHERE Age > 30 AND Salary > 50000;
ORAt least one condition must be TRUESELECT * FROM Employees WHERE Age < 30 OR Salary < 30000;
NOTReverses the result of a conditionSELECT * 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.

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
Subscribe to our channel & Don’t miss any update on trending technologies

Kick Start Your Career With Our Data Job

Master Fullstack Power BI – SQL, Power BI, Azure Cloud & Fabric Tools
Master in Data Science With Generative AI Transform Data into Business Solutions
Master Azure Data Engineering – Build Scalable Solutions for Big Data
Master AWS Data Engineering with Snowflake: Build Scalable Data Solutions
Transform Your Productivity With Low Code Technology: Master the Microsoft Power Platform

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/

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *