Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

SOLUTION FOR THE TEST

Round 1
Q1) For the given dataset (the entries continue), which of the following SQL query to returns first
name, last name of those employees who receive a higher salary than the employee with ID 163?
a) SELECT first_name, last_name
FROM employees
WHERE salary >
( SELECT salary
FROM employees
WHERE employee_id=163
);
b) SELECT firstname, lastname
WHERE employees
FROM salary >
( SELECT salary
WHERE employees
FROM employee_id=163
);
c) SELECT first_name, last_name
FROM employees
FROM salary >
( SELECT salary
WHERE employees
WHERE employee_id=163
);
d) WHERE first_name, last_name
SELECT employees
FROM salary >
( SELECT salary
FROM employees
WHERE employee_id=163
);

Ans: SELECT first_name, last_name


FROM employees
WHERE salary >
( SELECT salary
FROM employees
WHERE employee_id=163
);

Q2) Which SQL query returns first name, last name, department ID and job ID of employees who
have the same designation as the employee whose ID is 169
1
SOLUTION FOR THE TEST

a) SELECT first_name, last_name, salary,


FROM employees
WHERE job_id =
( SELECT manager_id
FROM employees
WHERE employee_id=169
);
b) SELECT first_name, last_name, salary, department_id, employee_id
FROM employees
WHERE job_id =
( SELECT job_id
FROM employees
WHERE job_id=169
);
c) SELECT first_name, last_name, salary, job_id, employee_id
FROM employees
WHERE job_id =
( SELECT job_id
FROM employees
WHERE job_id=169
);
d) SELECT first_name, last_name, salary, department_id, job_id
FROM employees
WHERE job_id =
( SELECT job_id
FROM employees
WHERE employee_id=169
);
Ans: SELECT first_name, last_name, salary, department_id, job_id
FROM employees
WHERE job_id =
( SELECT job_id
FROM employees
WHERE employee_id=169
);

Q3) Return all the fields of those employees who do not work in the departments where
managers’ IDs are between 100 and 200 (Begin and end values are included.).
a) SELECT department_id
FROM employees
WHERE department_id NOT IN
(SELECT department_id
FROM departments

2
SOLUTION FOR THE TEST

WHERE manager_id BETWEEN 100 AND 200);


b) SELECT *
FROM employees
WHERE department_id NOT IN
(SELECT department_id
FROM departments
WHERE manager_id BETWEEN 100 AND 200);
c) SELECT department_id
FROM employees
WHERE department_id IN
(SELECT department_id
FROM departments
WHERE manager_id NOT BETWEEN 100 AND 200);
d) SELECT *
WHERE employees
FROM department_id NOT IN
(SELECT department_id
FROM departments
WHERE manager_id BETWEEN 100 AND 200);
Ans: SELECT *
FROM employees
WHERE department_id NOT IN
(SELECT department_id
FROM departments
WHERE manager_id BETWEEN 100 AND 200);

Q4) Select ID, name, dept name, salary * 1.1 where instructor;
The query given below will give an error. Which one of the following has to be replaced to get
the desired output?
a) Salary*1.1
b) ID
c) Where
Ans: Where

Q5) What is a subquery?


a) A subquery is a select-from-where expression that is nested within another query
b) A subquery is any query that is nested within another query
c) A subquery is a relation that is externally specified which can be used to handle data in queries
Ans: A subquery is a select-from-where expression that is nested within another query

Q6) Which of the following is correct syntax for CASE statement?


a) CASE expression IS
WHEN choice_1 =

3
SOLUTION FOR THE TEST

Sequential_statements;
WHEN choice_2 =
Sequential_statements;
….
WHEN OTHERS =
Sequential_statements;
END CASE;
b) CASE expression IS
WHEN choice_1 =>
Sequential_statements;
WHEN choice_2 =>
Sequential_statements;
….
WHEN OTHERS =>
Sequential_statements;
END CASE;
c) CASE expression IS
IF choice_1 <=
Sequential_statements;
ELSIF choice_2 <=
Sequential_statements;
….
ELSIF OTHERS <=
Sequential_statements;
END CASE;
d) CASE expression IS
IF choice_1 =>
Sequential_statements;
ELSIF choice_2 =>
Sequential_statements;
….
ELSIF OTHERS =>
Sequential_statements;
END CASE;
Ans: CASE expression IS
WHEN choice_1 =>
Sequential_statements;
WHEN choice_2 =>
Sequential_statements;
….
WHEN OTHERS =>
Sequential_statements;
END CASE;

4
SOLUTION FOR THE TEST

Q7) Point out the wrong statement.


a) The ROW_NUMBER function simply assigns sequential numbering to the records of a result-set
or to the records within groups of a result-set
b) OVER clause is not required in all the ranking functions
c) SQL Server introduced four different ranking functions
d) All of the mentioned
Ans: OVER clause is not required in all the ranking functions

Q8) Which of the clause is not mandatory?


a) OVER clause
b) ORDER BY clause
c) PARTITION BY clause
d) All of the mentioned
Ans: PARTITION BY clause

Q9) Point out the wrong statement.


a) RANK() returns the rank of each row in the result set of partitioned column
b) DENSE_RANK() is same as RANK() function. Only difference is returns rank without gaps
c) NTILE() distributes the columns in an ordered partition into a specified number of groups
d) ROW_NUMBER() returns the serial number of the row order by specified column
Ans: NTILE() distributes the columns in an ordered partition into a specified number of groups

Q10) CASE is a sequential statement, which is similar to ________ concurrent statement.


a) CONCURRENT
b) HENCE
c) WHEN
d) THEN
Ans: WHEN

Q11) What should be the type of choices in CASE statement?


a) Boolean
b) Integer
c) Same as expression
d) No restriction on type
Ans: Same as expression

Q12) Consider a table "student_grades" . Which is the correct syntax to give each student a grade
sort the rows to have the highest grades on top. When score is 94 or higher, the row will have the
value of A. If the score is instead 90 or higher it will have t
a) SELECT *,
CASE
WHEN score >= 94 THEN "A"
WHEN score >= 90 THEN "A-"

5
SOLUTION FOR THE TEST

WHEN score >= 87 THEN "B+"


WHEN score >= 83 THEN "B"
WHEN score >= 80 THEN "B-"
WHEN score >= 77 THEN "C+"
WHEN score >= 73 THEN "C"
WHEN score >= 70 THEN "C-"
WHEN score >= 67 THEN "D+"
b) SELECT name,
CASE
WHEN score >= 94 THEN "A"
WHEN score >= 90 THEN "A-"
WHEN score >= 87 THEN "B+"
WHEN score >= 83 THEN "B"
WHEN score >= 80 THEN "B-"
WHEN score >= 77 THEN "C+"
WHEN score >= 73 THEN "C"
WHEN score >= 70 THEN "C-"
WHEN score >= 67 THEN "D
c) SELECT name,
CASE
WHEN score >= 94 THEN "A"
WHEN score >= 90 THEN "A-"
WHEN score >= 87 THEN "B+"
WHEN score >= 83 THEN "B"
WHEN score >= 80 THEN "B-"
WHEN score >= 77 THEN "C+"
WHEN score >= 73 THEN "C"
WHEN score >= 70 THEN "C-"
WHEN score >= 67 THEN "D
d) None of the mentioned
Ans: SELECT name,
CASE
WHEN score >= 94 THEN "A"
WHEN score >= 90 THEN "A-"
WHEN score >= 87 THEN "B+"
WHEN score >= 83 THEN "B"
WHEN score >= 80 THEN "B-"
WHEN score >= 77 THEN "C+"
WHEN score >= 73 THEN "C"
WHEN score >= 70 THEN "C-"
WHEN score >= 67 THEN "D

6
SOLUTION FOR THE TEST

Q13) What are the selectors in case of CASE statement?


a) Variable
b) Function
c) Expression
d) All of the mentioned
Ans: All of the mentioned

Q14) CASE statement uses which keyword to work like IF statement?


a) INTO
b) AS
c) WHEN
d) IN
Ans: WHEN

Q15) Examine the following SQL statement. What will be displayed in the query if the artistID is 14?
"SELECT ALBUM_TITLE,
CASE ARTIST_ID WHEN 14 THEN 'JOURNEY'
WHEN 27 THEN 'MEAT LOAF'
ELSE 'NOT FOUND'
END
FROM TBL_ARTIST;
a) JOURNEY
b) MEAT LOAF
c) AN ERROR IS RETURNED
d) NULL
Ans: AN ERROR IS RETURNED

Q16) What is a subquery?


a) A subquery is a select-from-where expression that is nested within another query
b) A subquery is any query that is nested within another query
c) A subquery is a relation that is externally specified which can be used to handle data in queries
d) A subquery is a condition that excludes all the invalid tuples from the database
Ans: A subquery is a select-from-where expression that is nested within another query

Q17) Consider the following query:


SELECT CustomerName, City, Country
FROM Customers
ORDER BY
(CASE
WHEN City IS NULL THEN Country
ELSE City
END);
Which of the following options is correct?

7
SOLUTION FOR THE TEST

a) The following SQL will order the customers by City. However, if City is NULL, then order by
Country
b) The following SQL will order the customers by City. However, if City is NULL, then error is
returned
c) The following SQL will order the customers by City. However, if City is NULL, then order by
nothing
d) None of the mentioned
Ans: The following SQL will order the customers by City. However, if City is NULL, then order by
Country

Q18) Which of the following should be used to find all the courses taught in the Fall 2009 semester
but not in the Spring 2010 semester
a) SELECT DISTINCT course id
FROM SECTION
WHERE semester = ’Fall’ AND YEAR= 2009 AND
course id NOT IN (SELECT course id
FROM SECTION
WHERE semester = ’Spring’ AND YEAR= 2010);
b) SELECT DISTINCT course_id
FROM instructor
WHERE name NOT IN (’Fall’, ’Spring’);
c) (SELECT course id
FROM SECTION
WHERE semester = ’Spring’ AND YEAR= 2010)
d) SELECT COUNT (DISTINCT ID)
FROM takes
WHERE (course id, sec id, semester, YEAR) IN (SELECT course id, sec id, semester, YEAR
FROM teaches
WHERE teaches.ID= 10101);
Ans: SELECT DISTINCT course id
FROM SECTION
WHERE semester = ’Fall’ AND YEAR= 2009 AND
course id NOT IN (SELECT course id
FROM SECTION
WHERE semester = ’Spring’ AND YEAR= 2010);

Q19) Which of the following is used to find all courses taught in both the Fall 2009 semester and in
the Spring 2010 semester .
a) SELECT course id
FROM SECTION AS S
WHERE semester = ’Fall’ AND YEAR= 2009 AND
EXISTS (SELECT *
FROM SECTION AS T

8
SOLUTION FOR THE TEST

WHERE semester = ’Spring’ AND YEAR= 2010 AND


S.course id= T.course id);
b) SELECT name
FROM instructor
WHERE salary > SOME (SELECT salary
FROM instructor
WHERE dept name = ’Biology’);
c) SELECT COUNT (DISTINCT ID)
FROM takes
WHERE (course id, sec id, semester, YEAR) IN (SELECT course id, sec id, semester, YEAR
FROM teaches
WHERE teaches.ID= 10101);
d) (SELECT course id
FROM SECTION
WHERE semester = ’Spring’ AND YEAR= 2010)
Ans: SELECT course id
FROM SECTION AS S
WHERE semester = ’Fall’ AND YEAR= 2009 AND
EXISTS (SELECT *
FROM SECTION AS T
WHERE semester = ’Spring’ AND YEAR= 2010 AND
S.course id= T.course id);

Q20) A Sub query is an SQL expression that is placed ________ another SQL statement.
a) Before
b) After
c) Inside
d) Outside
Ans: Inside

Q21) With which of the following statement(s) can the SQL sub queries be used?
a) SELECT
b) UPDATE
c) INSERT
d) All of the mentioned
Ans: All of the mentioned

Q22) Subqueries are not contained by which of the following clause?


a) FROM
b) WHERE
c) HAVING
d) both b and c
Ans: FROM

9
SOLUTION FOR THE TEST

Q23) Which statement is incorrect regarding subqueries?


a) Sub-queries should be provided with an alias
b) only list of records can be returned via subqueries
c) Sub Questions need not be surrounded by brackets ()
d) SELECT clauses contain subqueries
Ans: Sub-queries should be provided with an alias

Q24) Which of the following clause cannot be used in SQL sub queries?
a) GROUP BY
b) ORDER BY
c) DELETE
d) FROM
Ans: ORDER BY

Q25) Whenever a sub query appears in SQL, it is enclosed within ___________ and placed to the
_____ of the SQL operators.
a) Brackets, Left
b) Brackets, Right
c) Parenthesis, Left
d) Parenthesis, Right
Ans: Parenthesis, Right

Q26) Which of the following clause cannot be used in SQL sub queries?
a) GROUP BY
b) ORDER BY
c) DELETE
d) FROM
Ans: ORDER BY

Q27) The _________ operator cannot be used with the sub query, but within it.
a) IN
b) INTO
c) BETWEEN
d) JOIN
Ans: BETWEEN

Q28) Which of the following statements are False regarding subqueries?


a) Only two subqueries can be placed at one level
b) A subquery can appear on either side of a comparison operator
c) Both A and B
d) None of the above
Ans: Only two subqueries can be placed at one level

10
SOLUTION FOR THE TEST

Q29) Which of the following operators cannot be used in a sub-query?


a) AND
b) <
c) >
d) <>
Ans: AND

Q30) Consider two statements about outer and inner queries in context of SQL sub-queries?
i. The inner queries can get data from only one table
ii. The inner queries can get data from more than one table Which of the above statements are true?
a) Only i
b) Only ii
c) Both i and ii
d) None of the above
Ans: Only ii

Q31) Which of the following options will give correct output to find the Highest Salary by
Department?
a) WITH highest_salary AS (
SELECT first_name,
last_name,
department,
salary,
RANK () OVER (PARTITION BY department ORDER BY salary DESC) AS salary_rank
FROM employees)
SELECT first_name,
last_name,
salary,
department
FROM highest_salary
WHERE salary_rank = 1;
b) WITH highest_salary AS (
SELECT first_name,
last_name,
department,
salary,
RANK () OVER (PARTITION BY department ORDER BY salary DESC) AS salary_rank
FROM employees)
WHERE salary_rank = 1;
c) WITH highest_salary AS (
SELECT first_name,
last_name,
department,

11
SOLUTION FOR THE TEST

salary
FROM employees)
SELECT first_name,
last_name,
salary,
department
FROM highest_salary;
d) None of the above
Ans: WITH highest_salary AS (
SELECT first_name,
last_name,
department,
salary,
RANK () OVER (PARTITION BY department ORDER BY salary DESC) AS salary_rank
FROM employees)
SELECT first_name,
last_name,
salary,
department
FROM highest_salary
WHERE salary_rank = 1;

Q32) Which of the following options will give correct output to Find the Average Number of Orders?
a) WITH orders_count AS (
SELECT customer_id,
COUNT(*) AS no_of_orders
FROM orders
GROUP BY customer_id)
SELECT AVG(no_of_orders) AS avg_no_of_orders
FROM orders_count;
b) WITH orders_count AS (
SELECT customer_id,
COUNT(*) AS no_of_orders
FROM orders
)
SELECT AVG(no_of_orders) AS avg_no_of_orders
FROM orders_count;
c) WITH orders_count AS (
SELECT customer_id,
COUNT(*) AS no_of_orders
FROM orders
)
SELECT SUM(no_of_orders)

12
SOLUTION FOR THE TEST

FROM orders_count;
d) None of the above
Ans: WITH orders_count AS (
SELECT customer_id,
COUNT(*) AS no_of_orders
FROM orders
GROUP BY customer_id)
SELECT AVG(no_of_orders) AS avg_no_of_orders
FROM orders_count;

Q33) Which of the following is a set function?


a) RANK
b) NTILE
c) Window
d) All of the mentioned
Ans: All of the mentioned

Q34) Which of the function is not a ranking window function?


a) RANK
b) NTILE
c) ROW_NUMBER
d) None of the mentioned
Ans: RANK

Q35) Which of the following is correct to describe the difference Between Window Functions and
Aggregate Functions.
a) aggregate functions group multiple rows into a single result row &
window functions produce a result for each individual row
b) aggregate functions produce a result for each individual row &
window functions group multiple rows into a single result row
c) Both 1 and 2
d) None of the mentioned
Ans: aggregate functions group multiple rows into a single result row &
window functions produce a result for each individual row

Q36) To narrow a window function's application to a subgroup within a data set, use __________.
a) OVER
b) SUBGROUP
c) PARTITION BY
d) SECTION
Ans: PARTITION BY

13
SOLUTION FOR THE TEST

Q37) Which of the following options is not a method for numbering or ranking records?
a) NUMBER()
b) RANK()
c) ROW_NUMBER()
d) DENSE_RANK()
Ans: NUMBER()

Q38) Fill in the blank: SELECT SUM(qty) __________(PARTITION BY EmpID) AS EmpQty


a) ON
b) FOR
c) OVER
d) WINDOW
Ans: OVER

Q39) Which method for ranking records repeats a ranking when there is a tie and may have a result
set that is not consecutive?
a) RANK()
b) ROW_NUMBER()
c) DENSE_RANK()
d) NTILE()
Ans: RANK()

Q40) Which of the following options will give correct output to find the Number of Consecutive
Days With Order ?
a) WITH groupings_by_date AS (
SELECT c.id,
c.first_name,
c.last_name,
RANK() OVER (PARTITION BY c.id ORDER BY o.order_date) AS row_number,
o.order_date,
EXTRACT(DAY FROM o.order_date) - RANK() OVER (PARTITION BY c.id ORDER BY o.order_date) AS
date_group
FRO
b) WITH groupings_by_date AS (
SELECT c.id,
c.first_name,
c.last_name,
RANK() OVER (PARTITION BY c.id ORDER BY o.order_date) AS row_number,
o.order_date,
EXTRACT(DAY FROM o.order_date) - RANK() OVER (PARTITION BY c.id ORDER BY o.order_date) AS
date_group
FRO
c) WITH groupings_by_date AS (

14
SOLUTION FOR THE TEST

SELECT c.id,
c.first_name,
c.last_name,
RANK() OVER (PARTITION BY c.id ORDER BY o.order_date) AS row_number,
o.order_date,
EXTRACT(DAY FROM o.order_date) - RANK() OVER (PARTITION BY c.id ORDER BY o.order_date) AS
date_group
FRO
d) None of the mentioned
Ans: WITH groupings_by_date AS (
SELECT c.id,
c.first_name,
c.last_name,
RANK() OVER (PARTITION BY c.id ORDER BY o.order_date) AS row_number,
o.order_date,
EXTRACT(DAY FROM o.order_date) - RANK() OVER (PARTITION BY c.id ORDER BY
o.order_date) AS date_group
FROM customers c
JOIN orders o
ON c.id = o.customer_id )
SELECT id, first_name, last_name, COUNT(*) AS orders_in_row
FROM groupings_by_date
GROUP BY id, first_name, last_name, date_group;

15

You might also like