0% found this document useful (0 votes)
5 views7 pages

SQL Operators & Clauses

The document provides an overview of SQL clauses and operators, including the LIKE, IN, BETWEEN, AND, OR, NOT, ORDER BY, and DISTINCT operators. It includes syntax examples and descriptions for each operator, demonstrating how to use them in SQL queries. Additionally, it categorizes SQL operators into arithmetic, relational, and logical operators with their respective descriptions.

Uploaded by

faltu2655
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

SQL Operators & Clauses

The document provides an overview of SQL clauses and operators, including the LIKE, IN, BETWEEN, AND, OR, NOT, ORDER BY, and DISTINCT operators. It includes syntax examples and descriptions for each operator, demonstrating how to use them in SQL queries. Additionally, it categorizes SQL operators into arithmetic, relational, and logical operators with their respective descriptions.

Uploaded by

faltu2655
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SQL CLAUSE & OPERATORS

1. LIKE operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

LIKE operators with '%' and '_' wildcards.

Syntax
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;

LIKE Operator Description

WHERE CustomerName LIKE 'a%' Finds any values that start with "a"

WHERE CustomerName LIKE '%a' Finds any values that end with "a"

WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"

1. Display all customers with a CustomerName starting with "a".

SELECT * FROM Customers


WHERE CustomerName LIKE 'a%';

[Link] all customers with a CustomerName ending with "a".


SELECT * FROM Customers
WHERE CustomerName LIKE '%a';

[Link] CustomerName that starts with "a" and ends with "o".
SELECT * FROM Customers
WHERE ContactName LIKE 'a%o';

[Link] CustomerName that doesn’t contain "o".


SELECT * FROM Customers
WHERE ContactName NOT LIKE '%o%';
2. IN operator
The IN operator is a logical operator that allows you to test whether a specified value matches any value in a list.

Syntax:

SELECT column_name(s)
FROM table_name
WHERE column_name IN (list_of_values);
1. Find the Fname, Lname of the Employees who have Salary equal to 30000, 40000 or
25000.

SELECT Fname, Lname


FROM Employee
WHERE Salary IN (30000, 40000, 25000);

2. Find the Fname, Lname of all the Employee who have Salary not equal to 25000 or
30000.
SELECT Fname, Lname
FROM Employee
WHERE Salary NOT IN (25000, 30000);

3. Between Operator
The BETWEEN operator selects values within a given range. The values can be numbers, text,
or dates.
Syntax:

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Ex.1. List all the Employee Fname, Lname who salary in range of 30000 and 45000.

SELECT Fname, Lname


FROM Employee
WHERE Salary
BETWEEN 30000 AND 45000;
Ex.2. Find all the Employee name whose salary is not in the range of 30000 and 45000.

SELECT Fname, Lname


FROM Emplyoee
WHERE Salary
NOT BETWEEN 30000 AND 45000;
Ex.3. List all the Employee Fname, Lname who salary in range of 30000 and 45000.

SELECT Fname, Lname


FROM Employee
WHERE Salary
>= 30000 AND salary<=45000;

4. AND(&&), OR(||), NOT(!) Operator


AND (&&) - This operator displays only those records where both the conditions condition1
and condition2 evaluates to True.

Syntax
SELECT column1, column2, columnN
FROM table_name
WHERE [condition1] AND [condition2];

Example: Display id, name, salary of customers whose salary more than 2000 and
age less than 25.

ID | NAME | AGE | ADDRESS | SALARY |


+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------
SQL> SELECT ID, NAME, SALARY
FROM CUSTOMERS
WHERE SALARY > 2000 AND age < 25;

OUTPUT:
ID | NAME | SALARY |
+----+-------+----------+
| 6 | Komal | 4500.00 |
| 7 | Muffy | 10000.00

OR(||) - This operator displays the records where either one of the conditions
condition1 and condition2 evaluates to True.

Syntax
SELECT column1, column2, columnN
FROM table_name
WHERE [condition1] OR [condition2];

Ex. Display id, name, salary of customers whose salary more than 2000 or age less than 25.

SQL> SELECT ID, NAME, SALARY


FROM CUSTOMERS
WHERE SALARY > 2000 OR age < 25;
OUTPUT:
ID | NAME | SALARY |
+----+----------+----------+
| 3 | kaushik | 2000.00 |
| 4 | Chaitali | 6500.00 |
| 5 | Hardik | 8500.00 |
| 6 | Komal | 4500.00 |
| 7 | Muffy | 10000.00
5. Order by
The ORDER BY keyword is used to sort the result-set in ascending or descending order.

The ORDER BY keyword sorts the records in ascending order by default. To sort the records in
descending order, use the DESC keyword.

Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
[Link] all information from customer table on the basis of descending order of
salary.
SELECT * FROM Customers
ORDER BY Salary DESC;

5. Distinct
The SELECT DISTINCT statement is used to return only distinct (different) values. It is helpful
when there is a need of avoiding duplicate values present in any specific columns/table.
When we use distinct keyword only the unique values are fetched.

Syntax :

SELECT DISTINCT column1, column2


FROM table_name

Table – Student

ROLL_NO NAME ADDRESS AGE

1 RAM DELHI 18

2 RAMESH GURGAON 18

3 SUJIT ROHTAK 20

4 SURESH DELHI 18

3 SUJIT ROHTAK 20

2 RAMESH GURGAON 18
Ex.1.
SELECT DISTINCT NAME FROM Student;
Output :
NAME

Ram

RAMESH

SUJIT

SURESH

Ex.2.

SELECT DISTINCT ADDRESS FROM STUDENT;

OUTPUT:

DELHI

GURGAON

ROHTAK

SQL Operators
We can define operators as symbols that help us to perform specific mathematical and logical computations on
operands.
A + B here A and B is Operand , + is Operator
SQL operators have three different categories.
 Arithmetic operator
 Relational Operator
 Logical operator

Arithmetic operators: The Arithmetic Operators perform the mathematical operation on the numerical
data of the SQL tables.

[Link]. Operator Description

1 + The addition is used to perform an addition operation on the data values.

2 – This operator is used for the subtraction of the data values.

3 / This operator works with the ‘ALL’ keyword and it calculates division operations.
4 * This operator is used for multiply data values.

5 % Modulus is used to get the remainder when data is divided by another.

Relational Operators:
It is used to compare one expression’s value to other expressions. SQL supports different types of the
comparison operator, which is described below:

[Link]. Operator Description

1 = Equal to.

2 > Greater than.

3 < Less than.

4 >= Greater than equal to.

5 <= Less than equal to.

6 <> Not equal to.

Logical operators:
The Logical operators are those that are true or false. They return true or false values to combine one or more
true or false values.

[Link] Operator Description

Logical AND compares between two Booleans as expressions and returns true when both
1 AND expressions are true.

Logical OR compares between two Booleans as expressions and returns true when one
2 OR of the expressions is true.

Not takes a single Boolean as an argument and changes its value from false to true or
3 NOT from true to false.

You might also like