You are on page 1of 25

Subqueries

Subqueries
 A subquery is a query within a query.
 We can create subqueries within our SQL statements.
 These subqueries can reside in the WHERE clause, in the FROM clause, or in the
SELECT clause.
Employee table to understand Subquery in Oracle with Examples.
SQL Script to create and populate the Employee and Dept table with the required sample
data.
Example:

SELECT CustomerID AS ID
FROM Customers;
Example:

SELECT CustomerID AS ID,


CustomerName AS Customer
FROM Customers;
UPDATE (DML)
UPDATE: Command is used to update the column value in the table.

CREATE TABLE employees (


employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50)
);

Ques: Insert 3 entries in the table.


Ques:
Update the name of an employee with emploee_id 1.
UPDATE employees
SET first_name = 'NewFirstName', last_name = 'NewLastName'
WHERE employee_id = 1;
CREATE TABLE Customer(
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50),
Age int,
Phone int
);

desc Customer;

INSERT INTO Customer (CustomerID, CustomerName, LastName,


Country, Age, Phone)
VALUES (1, 'Shubham', 'Thakur', 'India','23','xxxxxxxxxx'),
(2, 'Aman ', 'Chopra', 'Australia','21','xxxxxxxxxx'),
(3, 'Naveen', 'Tulasi', 'Sri lanka','24','xxxxxxxxxx'),
(4, 'Aditya', 'Arpan', 'Austria','21','xxxxxxxxxx'),
(5, 'Nishant. Salchichas S.A.', 'Jain', 'Spain','22','xxxxxxxxxx');

Select * from Customer;


Update Single Column
Update the column NAME and set the value to ‘Nitin’ in the rows where the Age is 22.

Query—

UPDATE Customer SET CustomerName


= 'Nitin' WHERE Age = 22;
Output
Updating Multiple Columns

Update the columns NAME to ‘Satyam’ and Country to ‘USA’ where


CustomerID is 1.

Query:

UPDATE Customer
SET CustomerName = 'Satyam',
Country = 'USA‘
WHERE CustomerID = 1;
Output
Omitting WHERE Clause

If we omit the WHERE clause from the update query then all of the
rows will get updated.

Example:

UPDATE Customer SET CustomerName = 'Shubham';

Output---
Set Operators (UNION, UNION ALL, INTERSECT, MINUS )
1. Create a student table with the student id, name, and marks as attributes where the student id
is the primary key.
Ans.:
create table student
( student_id int primary key,
name varchar(20) not null,
marks decimal(5,2));

2. Insert the following details of students in the above table.


3. Find the min, max, sum, and average of the marks in a student marks table.
Ans.:
select max(marks), min(marks), sum(marks) , avg(marks) from student;

4. Write a SQL query to order the (student ID, marks) table in descending order of the marks.
Ans.:
select * from student order by marks desc;

5. Write a SQL query to display the marks without decimal places, display the reminder after diving marks by 3 and
display the square of marks.
Ans.:
select round(marks,0),mod(marks,3),pow(marks,2) from student;
Review
•UPPER(name) is used to convert the name to uppercase.
•LOWER(name) is used to convert the name to lowercase.
•SUBSTR(name, 1, 3) is used to extract the first 3 letters of the name.
•SUBSTR(name, -3) is used to extract the last 3 letters of the name.
•INSTR(name, 'A') is used to find the position of the letter 'A' in the name.

6. Write a SQL query to display names into capital letters, small letters, display first 3 letters of name,
display last 3 letters of name, display the position the letter A in name
Ans.:
select upper(name), lower(name), substr(name,1,3),substr(name,-3), instr(name,'a') from student;
7. Remove extra spaces from left, right and both sidesfrom the text - “ Informatics Practices Class XII "
Ans.:
select ltrim(‘ Informatics Practices Class XII ‘) ‘Left Spaces’, rtrim(‘
Informatics Practices Class XII ‘) ‘Right Trim’, trim(‘ Informatics Practices
Class XII ‘) from dual;

LTRIM -- To remove extra spaces from the left side of a text string.
RTRIM
TRIM
SELECT LTRIM(' Informatics Practices Class XII ') FROM dual;

SELECT RTRIM(' Informatics Practices Class XII ') FROM dual;

SELECT TRIM(' ' FROM ' Informatics Practices Class XII ') FROM dual;
8. Display today's date in "Date/Month/Year" format
Ans.:
select concat(date(now()), concat("/",concat(month(now()), concat("/",year(now())))));

9. Display dayname, monthname, day, dayname, day of month, day of year for today's date
Ans.:
select dayname(now()), monthname(now()), day(now()), dayname(now()), dayofmonth(now()),
dayofyear(now());

You might also like