BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

DBMS: SQL Operator

By Chaitanya Singh | Filed Under: DBMS

In this article, you will learn the various operators that can be used in SQL queries. Operators in SQL can be categorised as follows:

  1. Arithmetic operator
  2. Comparison operator
  3. Logical operator

1. SQL Arithmetic Operators

OperatorDescription
+Addition
_Subtraction
*Multiplication
/Division
%Modulus

Arithmetic Operator Examples:

This can be simply used to add two numbers:
SELECT 50 + 30;
Result: 80

Similarly subtraction operator can be used:
SELECT 30 - 20 -10;
Result: 0

Multiplication operator:
SELECT 10* 10 * 10;
Result: 1000

Division Operator: returns the quotient
SELECT 20 / 5;
Result: 4

Modulo operator: returns the remainder
SELECT 21 % 5;
Result: 1

2. SQL Comparison Operators

OperatorDescription
=Equal to operator
>Greater than operator
<Less than operator
>=Greater than or equal to operator
<=Less than or equal to operator
<>Not equal to operator

Comparison Operator Examples:

This will fetch all the records of employees from EMPLOYEE table where employee age is equal to 18:

SELECT * FROM EMPLOYEE
WHERE age = 18;

This will fetch the records of those employees from EMPLOYEE table who have salary greater than 10000:

SELECT * FROM EMPLOYEE
WHERE salary > 10000;

This will fetch the records of those employees from EMPLOYEE table who have salary less than 30000:

SELECT * FROM EMPLOYEE
WHERE salary < 30000;

This will fetch the records of employee who have salary equal to or greater than 15000:

SELECT * FROM EMPLOYEE
WHERE salary >= 15000;

This will fetch the records of employee who have salary equal to or greater than 25000:

SELECT * FROM EMPLOYEE
WHERE salary <= 25000;

This will fetch the records from EMPLOYEE table where the employee age is not equal to 18, which means it will get all the records except the records where employee age is 18:

SELECT * FROM EMPLOYEE
WHERE age <> 18;

3. SQL Logical Operators

OperatorDescription
ALLReturns true if all the subquery values fulfil the condition. This operator is generally use along with a subquery. Values generated by the subquery is used by the main query in where clause. Example will make it clear, refer example below.
ANDReturns if all the conditions that are separated by AND operator are true.
ANYReturns true if all the subquery values fulfil the condition.
EXISTSReturns true if the subquery returns one or more records.
BETWEENUsed in SQL query where we need to check the condition for values between a certain range, these ranges can be defined in where condition using BETWEEN operator.
INCompares the value to a specified list of values.
LIKEIt compares the value to similar values using wildcard operator.
OROR operator is used to combine multiple conditions in SQL query.
NOTUsed along with other logical operators and reverses their meaning for example, NOT IN will compares the values except the specified list of values.
SOMEReturns true if any of the subquery value fulfil the condition.

Logical Operator Examples:

The subquery will fetch the student ids for all the students from SCHOOL table where age is greater than 18, these ids will be used by main query that will fetch the name of these students from STUDENT table:

SELECT name FROM STUDENT
WHERE studentID = ALL (
SELECT studentID FROM SCHOOL WHERE age > 18
);

This will fetch the records of students where city is 'Agra' and country is 'INDIA'. This will only fetch those records where both of these conditions are true, for example it will not fetch the record where country is 'INDIA' but city is other than 'Agra':

SELECT * FROM STUDENT
WHERE City = "Agra" AND Country = "INDIA";

This will fetch all the records where student age lies between 18 and 22:

SELECT * FROM STUDENT
WHERE age BETWEEN 18 AND 22;

The following query demonstrate the use of NOT and LIKE operator. It will fetch the names of the students from STUDENT table where name does not start with letter 'a' or 'A':

SELECT name FROM STUDENT
WHERE name NOT LIKE 'a%';
❮ SQL CommandSQL CREATE ❯

Leave a Reply Cancel reply

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

DBMS Tutorial

Basics

  • DBMS Tutorial
  • DBMS Introduction
  • Database Applications
  • DBMS vs File System
  • DBMS vs RDBMS
  • DBMS Architecture
  • Three-level DBMS architecture
  • View in DBMS
  • Abstraction
  • Instance & Schema
  • DBMS languages

Data Models

  • Data Models
  • ER Diagram
  • ER Design issues
  • Convert ER to table
  • DBMS Generalization
  • DBMS Specialization
  • DBMS Aggregration
  • Relational Model
  • Hierarchical Model
  • Constraints
  • Cardinality

Relational Database

  • RDBMS concepts
  • Relational Algebra
  • Relational Calculus
  • Keys Index
  • Primary Key
  • Super Key
  • Candidate Key
  • Foreign Key
  • Composite Key
  • Alternate Key

Normalization

  • Normalization
  • Functional dependency

Transaction Management

  • Transaction Management
  • ACID properties
  • Transaction States
  • DBMS Schedules
  • Serializability
  • Conflict Serializability
  • View Serializability
  • Recoverability Of Schedule
  • Failure Classification
  • Log based Recovery
  • DBMS Checkpoint
  • Deadlock

Concurrency Control

  • Concurrency Control
  • Lock based protocol
  • Timestamp based protocol
  • Validation based protocol

File Organization

  • File Organization
  • Sequential File Organization
  • Heap File Organization
  • Hash File Organization
  • DBMS ISAM
  • B+ File Organization
  • Cluster File Organization

SQL Introduction

  • SQL Introduction
  • SQL Characteristics
  • Advantages of SQL
  • SQL Commands
  • SQL Operators
  • SQL CREATE
  • SQL DROP
  • SQL SELECT
  • SQL INSERT

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap