Oracle SQL Queries and Subqueries
Practice writing and understanding Oracle SQL queries including aggregate functions, subqueries, nested queries, and correlated subqueries
Questions
Find the odd one out of the following Oracle functions.
A. avg()
B. sqrt()
C. sum()
D. max()
E. count()
- avg()
- sqrt()
- sum()
- max()
- count()
Find the odd one out of the following SQL operations in ORACLE.
- date + number
- date - number
- date - date
- date + (number/24)
- none of the above
Predict the output of the following query.
Select INITCAP('Delhi is the capital city of INDIA') from DUAL
- Delhi is the capital city of INDIA
- Delhi is the capital city of India
- Delhi Is The Capital City Of INDIA
- Delhi Is The Capital City Of India
- Delhi Is The Capital City of India
SELECT STUDENT_ID, MARKS
FROM STUDENTS
WHERE STUDENT_ID = 119
What will be the output of the above query?
Assume STUDENT_ID to be a primary key column.
- There is a syntax error in the query.
- It may return one row if the STUDENT_ID 119 is present in the table else it will return zero rows.
- It always returns zero rows.
- It returns n number of rows, where n is the number of students whose STUDENT_ID is 119.
- It returns all the rows in the table irrespective of the data.
Which of the following statements will create a new user?
- CREATE USER kumar
- CREATE OR REPLACE USER kumar
- CREATE USER kumar IDENTIFIED BY indian
- CREATE NEW USER kumar IDENTIFIED BY indian
- CREATE OR REPLACE USER kumar IDENTIFIED BY indian
Write a query to display students details whose STUDENT_ID is 119 OR COURSE_ID is 301.
- SELECT *
FROM STUDENTS
WHERE STUDENT_ID = 119
OR COURSE_ID = 301 - SELECT ALL
FROM STUDENTS
WHERE STUDENT_ID = 119
OR COURSE_ID = 301 - SELECT STUDENT_ID, MARKS
FROM STUDENTS
WHERE STUDENT_ID = 119
OR COURSE_ID = 301 - SELECT ALL STUDENT_ID
FROM STUDENTS
WHERE STUDENT_ID = 119
OR COURSE_ID = 301 - All of the above
Write a query to choose all the students who got the highest marks in the exam.
- SELECT STUDENT_ID, MARKS
FROM STUDENTS
WHERE MAX(MARKS) - SELECT STUDENT_ID, MARKS
FROM STUDENTS
WHERE MARKS =
(
SELECT MAX(MARKS)
FROM STUDENTS
) - SELECT STUDENT_ID, MARKS
FROM STUDENTS
WHERE MARKS = MAX(MARKS) - SELECT STUDENT_ID, MARKS
FROM STUDENTS
WHERE MARKS = MAX - All of the above
Which of the following statements is TRUE about a subquery?
- Subqueries are mainly of two types - nested and correlated.
- A subquery usually have an outer query and an inner query.
- A subquery can be written using either a single or multiple tables.
- Generally, the innermost subquery is executed first and based on its results, outer queries will execute.
- All of the above
Write a query to display students details whose marks are greater than the student whose STUDENT_ID is 123.
- SELECT STUDENT_ID, MARKS
FROM STUDENTS
WHERE MARKS > 123 - SELECT STUDENT_ID, MARKS
FROM STUDENTS
WHERE MARKS > ( STUDENT_ID = 123 ) - SELECT STUDENT_ID, MARKS
FROM STUDENTS
WHERE MARKS > 123
(
SELECT MARKS
FROM STUDENTS
WHERE STUDENT_ID = 123
) - SELECT STUDENT_ID, MARKS
FROM STUDENTS
WHERE MARKS >
(
SELECT MARKS
FROM STUDENTS
WHERE STUDENT_ID = 123
) - All of the above
SELECT STUDENT_ID, MARKS
FROM STUDENTS
WHERE MARKS >
(
SELECT MARKS
FROM STUDENTS
WHERE STUDENT_ID = 123
)
Assume STUDENT_ID to be a primary key column.
- It always returns only one row with STUDENT_ID 123.
- It does not return any row as MARKS cannot be compared with STUDENT_ID.
- It returns n number of rows, where n is the number of students whose marks are greater than that of student whose STUDENT_ID is 123.
- It returns all the rows in the query.
- There is syntax error in the query.
SELECT STUDENT_ID, MARKS
FROM STUDENTS
WHERE MARKS > 80
What will be the output of the above query?
- There is a syntax error in this query.
- Only one row will be returned irrespective of the data.
- It always returns zero rows.
- It returns n number of rows, where n is the number of students whose marks are greater than 80.
- It returns all the rows in the table.
Which of the following statements is TRUE about a correlated subquery?
- Correlated subquery appears to be a nested subquery, but is little different.
- Correlated subquery is the one that is executed after the outer query.
- Execution of correlated subqueries is different from that of normal subqueries.
- Only 1 and 3 are correct.
- All the three statements are correct.
Write a query to choose all the students who have got the least marks in the exam.
- SELECT STUDENT_ID, MARKS
FROM STUDENTS
WHERE MIN(MARKS) - SELECT STUDENT_ID, MARKS
FROM STUDENTS
WHERE MARKS =
(
SELECT MIN(MARKS)
FROM STUDENTS
) - SELECT STUDENT_ID, MARKS
FROM STUDENTS
WHERE MARKS = MIN(MARKS) - SELECT STUDENT_ID, MARKS
FROM STUDENTS
WHERE MARKS = MIN - All of the above are correct.
Which of the following statements is TRUE about nested subquery?
- Nested subquery is one type of subquery whereas other one is a correlated subquery.
- A subquery is nested when you are having a subquery in the WHERE or HAVING clause of another subquery.
- For a nested subquery, execution starts from the innermost query and is based on its result, then the next inner query will execute and finally outer query will execute.
- Oracle allows upto 255 levels of subqueries.
- All of the above
Which of the following guidelines is correct for writing a subquery?
- Subquery must be enclosed in a paranthesis.
- Subquery must be placed on the right side of the comparison operator.
- When a subquery returns a null value, the outer query should not return any row.
- Indentation is very helpful in understanding a subquery.
- All of the above
Write a query to choose all the courses in which the least marks obtained is greater than the highest marks obtained in the COURSE_ID 201.
- SELECT COURSE_ID,
MIN(MARKS)
FROM STUDENTS
GROUP BY COURSE_ID
HAVING MIN(MARKS) >
(
SELECT MAX(MARKS)
FROM STUDENTS
WHERE COURSE_ID = 201
) - SELECT COURSE_ID,
MIN(MARKS)
FROM STUDENTS
WHERE MIN(MARKS) >
(
SELECT MAX(MARKS)
FROM STUDENTS
WHERE COURSE_ID = 201
) - SELECT COURSE_ID,
MIN(MARKS)
FROM STUDENTS
GROUP BY COURSE_ID
HAVING MIN(MARKS) > MAX(MARKS)
AND COURSE_ID = 201 - SELECT COURSE_ID,
MIN(MARKS)
FROM STUDENTS
GROUP BY COURSE_ID
WHERE MIN(MARKS) >
(
SELECT MAX(MARKS)
FROM STUDENTS
WHERE COURSE_ID = 201
) - All of the above
Write a query to get the details of all the students who are all enrolled in the same course as the student with STUDENT_ID 123.
- SELECT STUDENT_ID
FROM STUDENT
WHERE COURSE_ID = (
SELECT STUDENT_ID
FROM STUDENT
WHERE COURSE_ID = 123) - SELECT STUDENT_ID
FROM STUDENT
WHERE COURSE_ID = (
SELECT COURSE_ID
FROM STUDENT
WHERE STUDENT_ID = 123) - SELECT STUDENT_ID
FROM STUDENT
WHERE COURSE_ID = (
SELECT STUDENT_ID
FROM STUDENT
WHERE STUDENT_ID = 123) - SELECT STUDENT_ID
FROM STUDENT
WHERE STUDENT_ID = (
SELECT COURSE_ID
FROM STUDENT
WHERE COURSE_ID = 123) - None of the above
Write a query to display students details whose marks are greater than 80.
Note: Choose the most appropriate answer from the given choices.
- SELECT *
FROM STUDENTS
WHERE MARKS > 80 - SELECT ALL
FROM STUDENTS
WHERE MARKS > 80 - SELECT STUDENT_ID, MARKS
FROM STUDENTS
WHERE MARKS > 80 - SELECT ALL STUDENT_ID
FROM STUDENTS
WHERE MARKS > 80 - All of the above
Consider the following tables structucres:
STUDENT(STUDENT_ID, STUDENT_NAME, COURSE_ID, JOIN_DATE)
RESULTS(STUDENT_ID, SUB1_MARKS, SUB2_MARKS, SUB3_MARKS, SUB4_MARKS)
COURSE(COURSE_ID, COURSE_NAME)
Write a query to get the results of all the students who are all enrolled in the same course as the student with STUDENT_ID 123.
- SELECT * FROM RESULTS
WHERE STUDENT_ID IN (
SELECT STUDENT_ID
FROM STUDENT
WHERE COURSE_NAME = (
SELECT STUDENT_ID
FROM STUDENT
WHERE COURSE_ID = 123)) - SELECT * FROM RESULTS
WHERE STUDENT_ID IN (
SELECT STUDENT_ID
FROM STUDENT
WHERE COURSE_ID = (
SELECT STUDENT_ID
FROM STUDENT
WHERE COURSE_ID = 123)) - SELECT * FROM RESULTS
WHERE STUDENT_ID IN (
SELECT STUDENT_ID
FROM STUDENT
WHERE COURSE_ID = (
SELECT COURSE_ID
FROM STUDENT
WHERE STUDENT_ID = 123)) - SELECT * FROM RESULTS
WHERE STUDENT_ID IN (
SELECT STUDENT_ID
FROM STUDENT
WHERE COURSE_ID = (
SELECT STUDENT_ID
FROM STUDENT
WHERE COURSE_ID = 123)) - None of the above
Consider the following table structures:
STUDENTS (STUDENT_ID, STUDENT_NAME, COURSE_ID, JOIN_DATE)
RESULTS(STUDENT_ID, SUB1_MARKS, SUB2_MARKS, SUB3_MARKS, SUB4_MARKS)
COURSE(COURSE_ID, COURSE_NAME)
Find out the details of all the students who have appeared for more than 4 papers of the enrolled course.
- SELECT * FROM STUDENTS
WHERE COUNT < (
SELECT COUNT(*)
FROM RESULT
WHERE STUDENT_ID = STUDENT_ID) - SELECT * FROM STUDENTS S
WHERE 4 < (
SELECT COURSE_ID
FROM RESULT R
WHERE STUDENT_ID = R.STUDENT_ID) - SELECT * FROM STUDENTS S
WHERE COUNT < (
SELECT COUNT(*)
FROM RESULT
WHERE STUDENT_ID = STUDENT_ID) - SELECT * FROM STUDENTS S
WHERE 4 < (
SELECT COUNT(*)
FROM RESULT R
WHERE R.STUDENT_ID = S.STUDENT_ID) - None of the above