Professional Documents
Culture Documents
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:
desc Customer;
Query—
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:
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));
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 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());