0% found this document useful (0 votes)
9 views

8.SQL Operators

Sql

Uploaded by

BAJARANG
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

8.SQL Operators

Sql

Uploaded by

BAJARANG
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

NOTES

Structured Query Language (SQL)

IB

Notes by Ashish Barde


7972131295 | riseinstitute.tech

RISE Institute Mumbai, 797-213-1295, RiseInstitute.tech


Head Office – Akshar Building, Navi Mumbai – 410209 | New Branch – Murtijapur, Dt. Akola, Maharashtra – 444107.
©
SQL Operators…

1) Arithmatic

Notes by Ashish Barde


(+, - , , /)

2) Comparison (<, =, >, <=, >=, !=)

3) Logical (and , or, not)

4) Concatenation ( | | )
5) Like (% - Unknown length, _ - One Unknown Character)

6) Set (Union, Union all, Minus, Intersect)

7) in/ Not in
8) Between / Not between.

1. Arithmatic ( + , - , , / )
Arithmetic operations is a branch of mathematics, that involves the
study of numbers, operation of numbers that are useful in all the
other branches of mathematics. It basically comprises operations
such as Addition, Subtraction, Multiplication and Division.

RISE Institute Mumbai, 797-213-1295, RiseInstitute.tech


Head Office – Akshar Building, Navi Mumbai – 410209 | New Branch – Murtijapur, Dt. Akola, Maharashtra – 444107.
©
@ PRACTICAL @

Interview Questions ----- IMP


1) Display Total Salary (In-Hand) Sal+Bonus from Table ?
(Ask in Amdocs 1yr exp.)

ANS- Raw Data: insert one by one….

Notes by Ashish Barde


CREATE TABLE EMP_Info --------------------- Creating EMP_INFO Table
(EMP_ID NUMBER(10) PRIMARY KEY,
EMP_NAME VARCHAR2(50) NOT NULL,
EMP_SAL Number(10),
BONUS Number(8),
CITY VARCHAR2(50));

DESC EMP_info;

Raw Data:
INSERT INTO EMP_INFO VALUES (1,'Ashish', 92000, 11000, 'Navi Mumbai');
INSERT INTO EMP_INFO VALUES (2,'Amit', 83000, 19000, 'Pune');
INSERT INTO EMP_INFO VALUES (3,'Sumit', 77000, 15000, 'Mumbai');
INSERT INTO EMP_INFO VALUES (4,'Pratap', 78000, 16000, 'Nagpur');
INSERT INTO EMP_INFO VALUES (5,'Suprabhat', 82000, 14000, 'Akola');
INSERT INTO EMP_INFO VALUES (6,'Minal', 85000, 12000, 'Amravati');
INSERT INTO EMP_INFO VALUES (7,'Akshay', 75000, 16000, 'Nashik');
INSERT INTO EMP_INFO VALUES (8,'Sonam', 80000, 25000, 'Nagpur');
INSERT INTO EMP_INFO VALUES (9,'Ruksar', 85000, 17000, 'Navi Mumbai');
INSERT INTO EMP_INFO VALUES (10,'Neha', 60000, 22000, 'Mumbai');
INSERT INTO EMP_INFO VALUES (11,'Harish', 95000, 15000, 'Mumbai Thane');
INSERT INTO EMP_INFO VALUES (12,'Rani', 70000, 18000, 'Navi Mumbai Panvel');
RISE Institute Mumbai, 797-213-1295, RiseInstitute.tech
Head Office – Akshar Building, Navi Mumbai – 410209 | New Branch – Murtijapur, Dt. Akola, Maharashtra – 444107.
©
Select from emp_info;

Query Syntax: Try and Experience the difference in Report(o/p)

Select EMP_Sal + Bonus from emp_info; --- Basic Query

Notes by Ashish Barde


Select emp_name, (Emp_Sal+Bonus) from emp_info;

--- Better One (One of the type of report)

Select emp_info. , (Emp_Sal+Bonus) from emp_info;

---- Detailed report

SQL Aliases
SQL aliases are used to give a table, or a column in a
table, a temporary name.

Select (EMP_Sal + Bonus) as Total_Salary from emp_info;

Select emp_name, (Emp_Sal+Bonus) as Total_Salary from emp_info;

--- Better One (One of the type of report)

Select emp_info. , (Emp_Sal+Bonus)as Total_Salary from emp_info;

---- Detailed report

RISE Institute Mumbai, 797-213-1295, RiseInstitute.tech


Head Office – Akshar Building, Navi Mumbai – 410209 | New Branch – Murtijapur, Dt. Akola, Maharashtra – 444107.
©
Interview Questions ----- IMP
1. Alter table emp_info, rename the column Bonus to FINES
and display the Deducted Salary from Table…
(TCS Interview exp 4+ yrs.)

Notes by Ashish Barde


2. Comparision Operator
- It is a mathematical symbol which is used to compare two values.

 <, =, >, <=, >=, <>, !=

@ PRACTICAL @

Interview Questions ----- IMP

1. Display records whose salary is Greater than 70000?


2. Display records whose salary is Less than or equals to
85000?
3. Display Names whose Bonus Less than 15000?
4. Display emp_ID from table whose having salary Less than
95000?

RISE Institute Mumbai, 797-213-1295, RiseInstitute.tech


Head Office – Akshar Building, Navi Mumbai – 410209 | New Branch – Murtijapur, Dt. Akola, Maharashtra – 444107.
©
5. Display the info whose is from Pune?
6. Display City whose Salary is Greater than 75000?

1. ANS:-

Select from emp_info where emp_sal > 70000;

Notes by Ashish Barde


Select emp_name from emp_info where emp_sal > 70000;

Select emp_name, City, emp_sal from emp_info where


emp_sal > 70000;

____________________________________________________

3. Between Operator
---- It displays the possible range of values from column. It
applies on Numeric and date values.

1. Do u know Between Operator?

2. Display Records whose salary between 70000 to 95000.

Select from emp_info where EMP_Sal between 70000 and 95000;

RISE Institute Mumbai, 797-213-1295, RiseInstitute.tech


Head Office – Akshar Building, Navi Mumbai – 410209 | New Branch – Murtijapur, Dt. Akola, Maharashtra – 444107.
©
3. Display Records whose salary Not between 80000 to 90000.
Select from emp_info where EMP_Sal not between 80000 and 90000;

__________________________________________________________________

4. In Operator

Notes by Ashish Barde


– It displays the particular set of values from column. It applies
on Numeric, Alphabets and Date values.

1. Do u know In Operator?

2. Display the records whose salary is equal to 95000, 80000,


75000.
Select from emp_info where emp_sal in (95000, 80000,
75000);

3. Display Name whose ID is 4, 6 and 10.


Select from emp_info where emp_id in (4,6,10);
Select emp_name, emp_sal from emp_info where emp_id in
(4,6,10);

RISE Institute Mumbai, 797-213-1295, RiseInstitute.tech


Head Office – Akshar Building, Navi Mumbai – 410209 | New Branch – Murtijapur, Dt. Akola, Maharashtra – 444107.
©
Not In Operator – It applies on Numeric, Alphabets and Date
values

4. Display the records whose salary is equal to 95000, 80000,


75000.
Select from emp_info where emp_sal NOT IN (95000,

Notes by Ashish Barde


80000, 75000);

5. Difference between IN and BETWEEN operator.


____________________________________________________

5. Logical Operator
 SQL logical operators are used to test for the truth of the
condition. A logical operator like the Comparison operator returns
a boolean value of TRUE, FALSE, or UNKNOWN.

And , Or , Not…

RISE Institute Mumbai, 797-213-1295, RiseInstitute.tech


Head Office – Akshar Building, Navi Mumbai – 410209 | New Branch – Murtijapur, Dt. Akola, Maharashtra – 444107.
©
@ PRACTICAL @

Interview Questions ----- IMP

1. Display all the record having detailed ‘Amit and


‘83000’ from emp_info table? --(Mindtree
1yr +)
ANS-

Select from emp_info where emp_name = 'Amit' and emp_sal = 83000;

2. Display all record having detailed ‘Pratap’ and


‘Nagpur’ from emp_info table?

 Select from emp_info where emp_name = 'Pratap'


and City = 'Nagpur';

Notes by Ashish Barde


3. Want to display Employee Bonus who has no null
values in respective rows. ------- (IBM 3yr + exp.).
 Select from emp_info where not(Bonus is null);
4. Display the records whose either employee ID is 2 and
emp_name is Amit or Salary is 80000.
 Select from emp_info where (emp_id= 2 and
emp_name='Amit' ) or emp_sal =80000;

RISE Institute Mumbai, 797-213-1295, RiseInstitute.tech


Head Office – Akshar Building, Navi Mumbai – 410209 | New Branch – Murtijapur, Dt. Akola, Maharashtra – 444107.
©
6. Like Operator
 Most of the companies will ask question on like operator.
 It is used for pattern/String search.
 It is case sensitive.

Notes by Ashish Barde


There are 2 wild card keys.
 % Unknown length of string
 _ One Unknown character.

@ PRACTICAL @

Interview Questions ----- IMP

1. Display the rows whose name starts with 'R'.?


Ans: Select from emp_info where emp_name like 'R%';

2. Display the rows whose name ends with 'h'.


Ans: Select from emp_info where emp_name like '%h';

RISE Institute Mumbai, 797-213-1295, RiseInstitute.tech


Head Office – Akshar Building, Navi Mumbai – 410209 | New Branch – Murtijapur, Dt. Akola, Maharashtra – 444107.
©
3. Display the rows whose name's second char is 'a'?
Ans: Select from emp_info where emp_name like '_a';

4. Display the rows whose name's third char is 'i' ?


Ans: Select from emp_info where emp_name like '__i';

Notes by Ashish Barde


5. Display the rows whose name's third last char ends with 'r'?
Ans: Select from emp_info where emp_name like 'r__';

6. Display exactly 5 char string from name column.


Ans: Select from emp_info where emp_name like '_____';

7. Display the rows whose name contains 'a'


Ans: Select from emp_info where emp_name like '%a%';

8. Display the rows whose name contains 'a' or 'A'


Ans: Select from emp_info where emp_name like '%a%' or
emp_name like '%A%';

RISE Institute Mumbai, 797-213-1295, RiseInstitute.tech


Head Office – Akshar Building, Navi Mumbai – 410209 | New Branch – Murtijapur, Dt. Akola, Maharashtra – 444107.
©
9. Display the rows whose name Starts with Y and ends with f.
Ans Select from emp_info where emp_name like 'Y%' and
emp_name like '%f';

Notes by Ashish Barde


7. Concatenation ( | | )
 It combines the values of 2 column

@ PRACTICAL @

 Interview Questions ----- IMP

select from Information_Schema.Tables;

1. Display the Full Name from Table (FirstName+LastName)?


Ans:-
 Select Firstname ||' '|| Lastname from emp_info; ---
Simple one

RISE Institute Mumbai, 797-213-1295, RiseInstitute.tech


Head Office – Akshar Building, Navi Mumbai – 410209 | New Branch – Murtijapur, Dt. Akola, Maharashtra – 444107.
©
 Select Firstname ||' '|| Lastname as Fullname,
emp_sal, city from emp_info; ------- (Better for Report)

2. Display Salary and bonus together with concate


operator ?
 Select emp_sal || bonus from emp_info; --(No use)

Notes by Ashish Barde


 Select emp_sal ||' + '|| bonus as SalaryBonus from
emp_info;
 Select emp_sal ||'_'|| bonus as SalaryBonus from
emp_info;

@ ASSIGNMENTS @
Certainly! Here are some scenario-based interview questions on SQL
operators:

1. Arithmetic Operator Scenario:


- Scenario: You have a table named `Products` with columns `Price`
and `Quantity`. The marketing team wants a report showing the total
cost for each product (Price Quantity).
- Question: How would you use the arithmetic operator to calculate
and retrieve the total cost for each product in a SQL query?

RISE Institute Mumbai, 797-213-1295, RiseInstitute.tech


Head Office – Akshar Building, Navi Mumbai – 410209 | New Branch – Murtijapur, Dt. Akola, Maharashtra – 444107.
©
2. Comparison Operator Scenario:
- Scenario: In the `Orders` table, there's a column named
`OrderDate`. You need to find orders placed after January 1, 2023.
- Question: Using the comparison operator, provide a SQL query to
retrieve orders placed after January 1, 2023.

3. Logical Operator Scenario:


- Scenario: You have a table named `Employees` with columns
`Salary` and `YearsOfExperience`. HR wants a list of employees who
either have more than 5 years of experience or earn a salary above
$80,000.
- Question: How would you use logical operators to fulfill this
request in a SQL query?

4. Concatenation Operator Scenario:


- Scenario: The `Customers` table has separate columns for
`FirstName` and `LastName`. The marketing team wants a list of
customer names in the format "LastName, FirstName."
- Question: Using the concatenation operator, create a SQL query to
retrieve customer names in the desired format.

5. IN Operator Scenario:
- Scenario: Your e-commerce database has a table named `Products`
with a column named `Category`. You want to retrieve all products in
the 'Electronics' and 'Clothing' categories.
- Question: How would you use the `IN` operator to achieve this in a
SQL query?

RISE Institute Mumbai, 797-213-1295, RiseInstitute.tech


Head Office – Akshar Building, Navi Mumbai – 410209 | New Branch – Murtijapur, Dt. Akola, Maharashtra – 444107.
©
6. BETWEEN Operator Scenario:
- Scenario: In the `Employees` table, you need to find employees
with a salary between $50,000 and $70,000.
- Question: Use the `BETWEEN` operator in a SQL query to retrieve
employees with salaries in this range.

7. LIKE Operator Scenario:


- Scenario: The marketing team is looking for customers whose email
addresses start with "marketing."
- Question: How would you use the `LIKE` operator to find these
customers in the `Customers` table?

8. IS NULL Operator Scenario:


- Scenario: You suspect that there are records in the `Orders` table
with missing values in the `CustomerID` column. You want to identify
these records.
- Question: Use the `IS NULL` operator in a SQL query to retrieve
orders with missing `CustomerID` values.

9. NOT Operator Scenario:


- Scenario: You have a table named `Tasks` with a column `Status`.
You want a list of tasks that are not in a 'Completed' status.
- Question: How would you use the `NOT` operator to obtain a list of
tasks that are not marked as 'Completed'?

RISE Institute Mumbai, 797-213-1295, RiseInstitute.tech


Head Office – Akshar Building, Navi Mumbai – 410209 | New Branch – Murtijapur, Dt. Akola, Maharashtra – 444107.
©
10. Aggregate Functions Scenario:
- Scenario: In the `Sales` table, you want to find the average order
value for orders placed in the last month.
- Question: Combine an appropriate aggregate function with a date-
related condition in a SQL query to calculate the average order value for
the last month.

These scenario-based questions assess your ability to apply SQL


operators in real-world situations, testing both your understanding of the
operators and your problem-solving skills. 30. Display the rows whose
name Starts with Y and ends with f.

# For Practice | IMP Notes #

Notes by Ashish Barde

RISE Institute Mumbai, 797-213-1295, RiseInstitute.tech


Head Office – Akshar Building, Navi Mumbai – 410209 | New Branch – Murtijapur, Dt. Akola, Maharashtra – 444107.
©
Notes by Ashish Barde

RISE Institute Mumbai, 797-213-1295, RiseInstitute.tech


Head Office – Akshar Building, Navi Mumbai – 410209 | New Branch – Murtijapur, Dt. Akola, Maharashtra – 444107.
©
All the Best – Team RISE

RISE Institute Mumbai, 797-213-1295, RiseInstitute.tech


Head Office – Akshar Building, Navi Mumbai – 410209 | New Branch – Murtijapur, Dt. Akola, Maharashtra – 444107.
©

You might also like