Professional Documents
Culture Documents
Solution - Milestone Test 4 - DS3
Solution - Milestone Test 4 - DS3
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
);
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
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
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
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
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
6
SOLUTION FOR THE TEST
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
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
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
9
SOLUTION FOR THE TEST
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
10
SOLUTION FOR THE TEST
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;
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()
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