Introduction To SQL Test Your Understanding
Introduction To SQL Test Your Understanding
Attempt #1
Jun 08, 10:13 PM
Marks: 1
Q No: 1
Correct Answer
Marks: 1/1
How are Python and SQL different?
Python is a programming language and cannot be used for data manipulation, whereas SQL can be used for
data manipulation.
Python can store data in the form of a database.
SQL is a query language that is used along with a database to store and maintain data, whereas Python is a
programming language.
You Selected
Attempt History
Attempt #1
Jun 09, 8:42 AM
Marks: 2
Q No: 1
Correct Answer
Marks: 1/1
Which of the following clauses is used to fetch unique records from a table?
DIFFERENT
DISTINCT
You Selected
UNIQUE
NEW
Q No: 2
Correct Answer
Marks: 1/1
Which of the following queries can be used to fetch the first 5 rows from a table?
(consider the table name as STUDENT)
SELECT * FROM STUDENT LIMIT 5
You Selected
SELECT 5 * FROM STUDENT
SELECT TOP 5 * FROM STUDENT
LIMIT 5 SELECT * FROM STUDENT
The LIMIT command is used to display the first few records from the table.
It should be used at the end while writing a SQL query.
Q No: 3
Incorrect Answer
Marks: 0/1
Which of the following options is correct regarding the below SQL query? (Consider the
Query Error
Correct Option
Creates a new column in the student table and fills the column with the value equal to marks + 5
Updates all the marks of students by adding 5 to the existing values
You Selected
The syntax for the UPDATE command is: UPDATE [table_name] SET [expression].
In the above query, the SET keyword is missing, and hence the query is erroneous.
Q No: 3
Correct Answer
Marks: 1/1
Which of the following options is correct regarding the below SQL query? (Consider the table
name as STUDENT)
The syntax for the UPDATE command is: UPDATE [table_name] SET [expression].
In the above query, the SET keyword is missing, and hence the query is erroneous.
Attempt History
Attempt #2
Jun 09, 11:09 AM
Marks: 2
Q No: 1
Correct Answer
Marks: 1/1
Which condition can be used in the blank space in the below query to fetch the details
of all customers whose first name contains "B" as the third character and ends with "S"?
IS %B%S
LIKE __B%S
You Selected
LIKE %B%S
LIKE ___B__S
Underscore(_) is used to match a single character, and % can be used to match any
number of characters.
So for the third character to be B, the pattern should be __B and to end with S, the
pattern will be __B%S.
% is used after B as it is not given how many characters can be thereafter B and before
S.
Q No: 2
Correct Answer
Marks: 1/1
Which of the following is true regarding the BETWEEN clause?
Both the upper and lower limits are excluded.
The upper limit is excluded.
The lower limit is excluded.
Both the upper and lower limits are included.
You Selected
The BETWEEN clause returns the result set which contains all the values matching the
specified range, including the upper and lower limit.
E.g. SELECT * FROM EMPLOYEES WHERE SALARY BETWEEN 1000 and 5000
returns all the employees whose salary is >=1000 and <=5000
Attempt History
Attempt #1
Jun 10, 4:22 PM
Marks: 5
Q No: 1
Correct Answer
Marks: 1/1
Which of the following is the correct query to get all shipper details from the shipper
table?
SELECT *
FROM shipper;
You Selected
SELECT ALL
FROM shipper;
SELECT *
FROM shipper
The "*" in SELECT * means "fetch all the columns," and FROM signifies from where
you need to fetch the data or from what table you need to fetch the data. You should
end each query with a semicolon (;) so that SQL understands it as the end of the code.
Option 1 is correct, as SELECT * will fetch all the columns from the shipper table.
Option 2 is incorrect, as SELECT ALL will not fetch all the columns from the shipper
table.
Option 3 is incorrect as the query does not end with a semicolon.
Q No: 2
Correct Answer
Marks: 1/1
Which of the following is the correct query to find out the name and phone numbers of
You Selected
SELECT shipper_phone, shipper_name
FROM shipper;
SELECT shipper_name shipper_phone
FROM shipper;
SELECT fetches only the mentioned columns and according to the order in which you
specify the columns, which are: the shipper_name and shipper_phone. The column
names are separated with a comma (,) in the SELECT statement.
Option 1 is correct, as it will fetch the mentioned columns from the shipper table in the
required order.
Option 2 is incorrect, as it will not fetch the mentioned columns from the shipper table in
the required order.
Option 3 is incorrect, as the columns are not specified using a comma in the SELECT
clause.
Q No: 3
Correct Answer
Marks: 1/1
Which of the following is the correct query to find out unique cities from the address
table
You Selected
SELECT UNIQUE city
FROM address;
SELECT city
FROM address
LIMIT 1;
The DISTINCT keyword in the SELECT is used to return only distinct or unique values
from the mentioned columns.
The LIMIT keyword is used to specify the number of records to be returned.
Option 1 is correct, as it will fetch the distinct or unique values from the city column.
Option 2 is incorrect, as it will not the distinct/unique values from city column as
UNIQUE is not a valid here.
Option 3 is incorrect, as it will return only 1 record from the city column.
Q No: 4
Correct Answer
Marks: 1/1
Which of the following is the correct query to find out unique city and state combination
You Selected
SELECT DISTINCT state, DISTINCT city
FROM address
Adding more than 1 column in SELECT with DISTINCT will fetch the unique
combinations of the mentioned columns.
Option 1 is correct, as it will fetch the distinct or unique values for city and state
combinations.
Option 2 is incorrect, as two DISTINCT are not valid in MySQL and a semicolon is
missing at the end of the query.
Q No: 5
Correct Answer
Marks: 1/1
Which of the following is the correct query to find out first 5 customer_username,
You can choose the number of records to display in the output by changing the number
in the LIMIT clause.
Option 1 is correct, as it will fetch the first 5 records for the mentioned columns in the
required order.
Option 2 is incorrect, as it will not fetch the first 5 records for the mentioned columns in
the required order.
Option 3 is incorrect, as it will fetch the first 5 unique records of the combinations of the
mentioned columns.
Attempt History
Attempt #1
Jun 10, 5:17 PM
Marks: 11
Q No: 1
Correct Answer
Marks: 1/1
Which of the following is the correct query to get all the cities of USA from address
table?
SELECT city
FROM address
WHERE country='USA';
You Selected
SELECT city
FROM address
WHERE country='Usa';
SELECT city
FROM address
WHERE country=USA;
The WHERE clause filters the records that satisfy the given condition. The condition
that you give in WHERE is case-sensitive. Also, if you are giving a condition on a
text/string column, it should be specified within " ".
Option 1 is correct, as it will fetch all the cities from the "USA."
Option 2 is incorrect, as the condition mentioned will filter the records for "Usa" and not
"USA."
Option 3 is incorrect, as the condition is not specified within quotes ("").
Q No: 2
Correct Answer
Marks: 1/1
Which of the following is the correct query to get all the cities from USA and India from
the address table?
SELECT city
FROM address
WHERE country IN ('USA','India');
You Selected
SELECT city
FROM address
WHERE country IN ('USA')
WHERE country IN ('India');
SELECT city
FROM address
WHERE country ='USA'
WHERE country ='India';
You can input a list of values in the WHERE condition using the IN statement. The list
may contain even one value.
More than one WHERE CLAUSE are not allowed/valid in MySQL.
Option 1 is correct, as it will fetch all the cities from USA and India.
Option 2 is incorrect, as it has two WHERE clause it it, that is not valid in SQL.
Option 3 is incorrect, as it has two WHERE clause it it, that is not valid in SQL.
Q No: 3
Correct Answer
Marks: 1/1
Which of the following is the correct query to get all get all the records where the city
name starts with the letter "A" from the address table?
SELECT city
FROM address
WHERE city LIKE "A%";
You Selected
SELECT city
FROM address
WHERE city LIKE "%A";
SELECT city
FROM address
WHERE city LIKE "a%";
You Selected
SELECT city
FROM address
WHERE city LIKE = "A%";
The LIKE operator is used to search for a specific pattern in the string/text columns and
it is The LIKE operator is case-insensitive. "=" operator is not required in LIKE operator
in WHERE condition.
Option 1 is correct, as "A%" will fetch the cities that start with "A" and are followed by
any number of characters.
Option 2 is incorrect, as "A%" will fetch the cities that end with A.
Option 3 is correct, as "a%" will fetch the cities that start with A since LIKE is case-
insensitive.
Option 4 is incorrect, as the "=" operator is not valid in the LIKE clause.
Q No: 4
Correct Answer
Marks: 1/1
Which of the following is the correct query to get all get all the records where the city
name ends with the letter "r" from the address table?
SELECT city
FROM address
WHERE city LIKE "%R";
You Selected
SELECT city
FROM address
WHERE city LIKE "r%";
SELECT city
FROM address
WHERE city LIKE = "r%";
The LIKE operator is used to search for a specific pattern in the string/text columns and
it is The LIKE operator is case-insensitive. "=" operator is not required in LIKE operator
in WHERE condition.
Option 1 is correct, as "A%" will fetch the cities that end with "R" or "r" and are followed
by any number of characters.
Option 2 is incorrect, as "r%" will fetch the cities that start with "r" or "R."
Option 3 is incorrect, as the "=" operator is not valid in the LIKE clause.
Q No: 5
Correct Answer
Marks: 1/1
Which of the following is the correct query to get all get all the records where the city
name starts with "K" and ends with the letter "r" from the address table?
SELECT city
FROM address
WHERE city LIKE "K%r"
You Selected
SELECT city
FROM address
WHERE city LIKE "r%K"
SELECT city
FROM address
WHERE city LIKE "K%"
WHERE city LIKE "%r";
The LIKE operator is used to search for a specific pattern in the string/text columns and
it is The LIKE operator is case-insensitive. "=" operator is not required in LIKE operator
in WHERE condition.
Option 1 is correct, as "K%r" will fetch the cities that start with "K" and end with "r," with
any number of characters in between.
Option 2 is incorrect, as "r%K" will fetch the cities that start with "r" and end with "K,"
with any number of characters in between.
Option 3 is incorrect, two WHERE clause are not valid in SQL.
Q No: 6
Correct Answer
Marks: 1/1
Which of the following is the correct query to get all get all the records where the city
name starts with "A" and consists of 4 characters from the address table?
SELECT city
FROM address
WHERE city LIKE "A___";
You Selected
SELECT city
FROM address
WHERE city LIKE "A__";
SELECT city
FROM address
WHERE city LIKE "____";
SELECT city
FROM address
WHERE city LIKE A___;
The "_" operator in the LIKE clause is also used to find patterns in the strings. "_"
represents a single character in a string.
Option 1 is correct, as "A___" will fetch the cities that start with "A" and have exactly 3
letters after "A."
Option 2 is incorrect, as "A___" will fetch the cities that start with "A" and have exactly 2
letters after "A."
Option 3 is incorrect, as "____" will fetch the cities that have exactly 4 letters but start
with any letter.
Option 4 is incorrect, as the pattern in the LIKE clause is not mentioned within quotes
("").
Q No: 7
Correct Answer
Marks: 1/1
Which of the following is the correct query to get all get all the records where the city
name starts with with "M" and consists of 5 or more characters from address table ?
SELECT city
FROM address
WHERE city LIKE "M____%";
You Selected
SELECT city
FROM address
WHERE city LIKE "M_____%";
SELECT city
FROM address
WHERE city LIKE M_____%";
"M____%" will fetch the city names that start with "M" and have exactly 4 characters
after "M," and it may have any number of characters after 5.
Option 1 is correct, as "M____%" will fetch the city names that start with "M" and have
exactly 4 characters after "M," and it may have any number of characters after 5.
Option 2 is incorrect, as "M_____%" will fetch the city names that start with "M" and
have exactly 5 characters after "M".
Option 3 is incorrect, as the pattern is not mentioned within the quotes ("").
Q No: 8
Correct Answer
Marks: 1/1
Which of the following is the correct query to get all get all the records that belong to
"USA" country and "NY" state from address table ?
SELECT *
FROM address
WHERE state='NY'
AND country='USA';
You Selected
SELECT *
FROM address
WHERE state='NY'
country='USA';
SELECT *
FROM address
WHERE state='USA'
AND country='NY';
SELECT *
FROM address
WHERE state= USA
AND country='NY';
You can add multiple conditions in the WHERE clause using the AND operator and filter
out the records that satisfy both of the mentioned conditions.
Option 1 is correct, as it will fetch all the records that belong to "NY" state and "USA."
Option 2 is incorrect, as these two conditions are specified in the WHERE clause
without any operator like AND.
Option 3 is incorrect, as these conditions are incorrect.
Option 4 is incorrect, as these conditions are incorrect.
Q No: 9
Correct Answer
Marks: 1/1
Which of the following is the correct query to get all get all the records that belong to
either "USA" country or "NY" state from address table ?
SELECT *
FROM address
WHERE state='NY'
OR country='USA' ;
You Selected
SELECT *
FROM address
WHERE state='NY'
AND country='USA' ;
SELECT *
FROM address
WHERE state= NY
AND country='USA' ;
You can add multiple conditions to the WHERE clause using the OR operator as well
and filter out the records that satisfy either of the mentioned conditions.
Option 1 is correct, as it will fetch all the records that belong to the "NY" state or the
"USA."
Option 2 is incorrect, as the AND will fetch the records that satisfy both conditions.
Option 3 is incorrect, as these conditions are incorrect.
Q No: 10
Correct Answer
Marks: 1/1
Which of the following is the correct query to get all get all the products that have
product_quantity_avail of more than 10 from product table ?
SELECT *
FROM product
WHERE product_quantity_avail > 10;
You Selected
SELECT *
FROM product
WHERE product_quantity_avail >= 10;
SELECT *
FROM product
WHERE product_quantity_avail < 10;
SELECT *
FROM product
WHERE product_quantity_avail = 10;
Option 1 is correct, as the ">" operator will fetch those records that have
product_quantity_avail greater than 10.
Option 2 is incorrect, as ">=" operator will also fetch those records that
have product_quantity_avail equals to 10 apart from greater than 10.
Option 3 is incorrect, as the "<" operator will also fetch those records that have
product_quantity_avail less than 10.
Option 4 is incorrect, as "<=" operator will also fetch those records that
have product_quantity_avail equals to 10 apart from lesser than 10.
Q No: 11
Correct Answer
Marks: 1/1
Which of the following is the correct query to get all get all the products that have
product_quantity_avail between 10 and 50.from product table ?
SELECT *
FROM product
WHERE product_quantity_avail BETWEEN 10 AND 50;
You Selected
SELECT *
FROM product
WHERE product_quantity_avail>=10 AND product_quantity_avail<50;
SELECT *
FROM product
WHERE product_quantity_avail>10 AND product_quantity_avail<=50;
SELECT *
FROM product
WHERE product_quantity_avail>10 AND product_quantity_avail<50;
SELECT *
FROM product
WHERE product_quantity_avail BETWEEN 10 OR 50;
The BETWEEN operator selects values within a given range. The values can be
numbers, text, or dates. This operator is inclusive; the begin and end values are
included.
Option 1 is correct, as it will fetch the records that have product_quantity_avail from 10
to 50.
Option 2 is incorrect, as it will not fetch the records that have product_quantity_avail
equal to 50.
Option 3 is incorrect, as it will not fetch the records that have product_quantity_avail
equal to 10.
Option 4 is incorrect, as it will not fetch the records that have product_quantity_avail
equal to 10 and 50.
Option 5 is incorrect, as OR is not allowed with the BETWEEN clause.
2.
3.
4.
5.
6.
Attempt History
Attempt #1
Jun 14, 10:17 PM
Marks: 9
Q No: 1
Correct Answer
Marks: 1/1
What does the query SELECT 'a'; do?
This code would fail to run as there is no table mentioned
This gives you a result with a single column named 'a' with a value of 'a'
You Selected
SELECT statements do not necessarily need tables. SELECT can be used to get any value or array of values.
For example, to learn more, run the queries listed below.
Q No: 2
Correct Answer
Marks: 1/1
What does the query SELECT * FROM table WHERE 1=2; do?
It returns no result
You Selected
It returns all the rows from the table
It returns only the 1st and 2nd rows in the table
It returns all the rows where the 1st column value is equal to the 2nd column value
In step #1, 1=2 is a condition that will never be satisfied, and so no rows will be returned.
Q No: 3
Correct Answer
Marks: 1/1
What is the SQL element that is used to temporarily rename a column or table heading?
ALIAS
You Selected
CHARACTER
SYMBOL
IDENTITY
A table or a column name in a table can be given a temporary name using SQL aliases.
Example:
This query will rename the column Student_ID as ID and Student_Name as Student.
Q No: 4
Correct Answer
Marks: 1/1
Is the system going to give an error while running the below code?
No
You Selected
Yes
The system will not give you an error. By running the above query, it will give the output as "Null".
Q No: 5
Correct Answer
Marks: 1/1
We can retrieve unique values by using the "UNIQUE" function in the SELECT statement.
True
False
You Selected
In MYSQL, we can select unique values with the help of a "DISTINCT" function.
Q No: 6
Correct Answer
Marks: 1/1
Questions from 6 to 10 require you to run MYSQL queries to give answers. So, kindly import the
data telecom_customer_t.sql into MYSQL if you have not already imported it.
[Q] What does SELECT * FROM customer_t WHERE customer_age <> 20; do?
It will fetch all the details of the customer whose age is 20.
Syntax error.
It will fetch all the details of the customer regardless of their age
It will fetch all the details of the customer whose age is not equal to 20.
You Selected
The symbol "<>" denotes "not equal to". The above query will fetch all the columns from the customer_t table
where the customer age is not equal to 20.
Q No: 7
Correct Answer
Marks: 1/1
Which of the following queries will fetch the details of the customer whose age is between 15 and 25?
SELECT * FROM customer_t WHERE customer_age IN 15 AND 25;
SELECT * FROM customer_t WHERE customer_age BETWEEN 15 AND 25;
You Selected
SELECT * FROM customer_t WHERE customer_age BETWEEN (15,25);
SELECT * FROM customer_t WHERE customer_age IN(15,25);
This query will fetch the details of the customer whose age is between 15 and 25 using the "Between"
operator.
This query will fetch the details of the customers whose age is either 15 or 25 since we are using the "IN"
operator.
Both the queries will not provide any results as both the queries are syntactically incorrect.
Q No: 8
Incorrect Answer
Marks: 0/1
Which of the following query will fetch the details of the customer whose city is Chicago?
SELECT * FROM customer_t WHERE city LIKE '*C*';
SELECT * FROM customer_t WHERE city LIKE '%Chicago';
Correct Option
SELECT * FROM customer_t WHERE city == 'Chicago';
SELECT * FROM customer_t WHERE city LIKE '%C';
You Selected
This query will fetch the details of the customer whose city name has the last letter "C".
The second query will fetch the details of the customer whose city is Chicago.
Both the queries will not provide any results as both the queries are syntactically incorrect.
Q No: 9
Correct Answer
Marks: 1/1
Which of the following queries will fetch the details of the customer who is not from the city of New York?
SELECT * FROM customer_t WHERE city = ('Chicago','Los Angeles');
SELECT * FROM customer_t WHERE city NOT IN 'New York';
SELECT * FROM customer_t WHERE city =! 'New York';
SELECT * FROM customer_t WHERE city <> 'New York';
You Selected
This query will fetch the details of the customer who is not from the city of New York. The condition has been
explained in the where clause.
The above query is incorrect as this is not the correct way to use NOT IN. The correct code is given
as: (SELECT * FROM customer_t WHERE NOT city = 'New York';)
The above query will not give the desired results as we cannot compare two values using the "=" operator. The
correct code is given as: (SELECT * FROM customer_t WHERE city = 'Chicago'or city ='Los Angeles';)
Q No: 10
Correct Answer
Marks: 1/1
Which of the following queries will fetch the details of the customer whose tenure is 1 or 2?
SELECT * FROM customer_t WHERE tenure = 1 OR 2;
SELECT * FROM customer_t WHERE tenure = 1 OR tenure = 2;
You Selected
SELECT * FROM customer_t WHERE tenure = (1,2);
SELECT * FROM customer_t WHERE tenure = 1 AND 2;
The correct result can be provided by using the "OR" operator. This query is the correct form for how to use
the OR operator in the where clause.
The above query is the incorrect way to compare the values using the AND operator.
The above query is the incorrect way to compare the values using the OR operator.
The above query is the incorrect way to compare the values using the "=" operator.
Correct Answer
Marks: 1/1
An HR manager of a company wants to know how many employees are working in each
department. The data about the employees and their department names are in the
Employee table. Which of the below queries can the HR manager write to accomplish
the task?
SELECT dept_name, COUNT(*) FROM employee
SELECT dept_name, COUNT(*) FROM employees GROUP BY dept_name
SELECT dept_name, COUNT(emp_id) FROM employee GROUP BY dept_name
You Selected
SELECT dept_name, SUM(emp_id) FROM employee GROUP BY dept_name
To fetch the count of employees in each department, the COUNT() function can be used.
COUNT(*) and COUNT(emp_id) will fetch the count of employees excluding nulls. GROUP
BY is used to get the count of employees in each department.
Q No: 2
Correct Answer
Marks: 1/1
A shipping company owner is interested in knowing the total number of delivered
orders. The data for the orders and delivery status is present in a table called
"ORDERS." Which of the below query can't display the result for the same?
SELECT COUNT(*) FROM orders where delivery_status = "delivered"
SELECT SUM(*) FROM orders WHERE delivery_status = "delivered"
You Selected
SELECT COUNT(*) FROM orders WHERE delivery_status IN ('delivered')
SELECT SUM(CASE WHEN delivery_status = 'delivered' THEN 1 ELSE 0 END) AS
NO_OF_DELIVERIES FROM orders
SELECT SUM(CASE WHEN delivery_status = 'delivered' THEN 1 ELSE 0 END) AS
NO_OF_DELIVERIES FROM orders
When the query is executed, the CASE WHEN statement checks for the value of
delivery status. If it is equal to 'delivered' then it creates a column called
NO_OF_DELIVERIES and adds 1 to that column, else 0. Finally, SUM calculates the
total of all the ones assigned by the CASE WHEN statement and returns the count.
SELECT COUNT(*) FROM orders where delivery_status = "delivered"
When the query is executed, all the rows FROM orders table where the delivery_status
is equal to 'delivered' are retrieved. COUNT(*) calculates the total number of rows for
the fetched data.
SELECT COUNT(*) FROM orders WHERE delivery_status IN ('delivered')
When the query is executed, all the rows FROM the orders table where the
delivery_status matches any of the values present in the IN clause are
retrieved. COUNT(*) calculates the total number of rows for the fetched data.
Aggregation Quick Practice
Type
:
Practice Quiz
Questions
:
10
Scoring Policy
:
Highest Score
Your Marks
:
10/10
RETAKE
Attempt History
Attempt #1
Jun 16, 3:16 PM
Marks: 10
Q No: 1
Correct Answer
Marks: 1/1
SELECT SUM(5)
FROM product;
300
You Selected
5
60
Error
SUM() is an aggregation function that sums up numeric values and returns the single value at a given level.
The most common aggregate functions are SUM, AVG, MIN, MAX, and COUNT. are used to perform a
calculation on a set of values and return a single result.
This query will create a new column with 5 as values and return the sum of 5 for each row. If the table
has n rows, the result would be n * 5.
Q No: 2
Correct Answer
Marks: 1/1
Which of the following is the correct query to get the total inventory available from the product table?
SELECT SUM(product_quantity_avail) AS total_stock
FROM product;
You Selected
SELECT COUNT(product_quantity_avail) total_stock
FROM product;
The SUM() function returns the sum of the numeric column specified.
Option 1 is correct, as this will fetch the total inventory from the product table.
Option 2 is incorrect, as this will fetch the number of rows instead of the total inventory from the product table.
Q No: 3
Correct Answer
Marks: 1/1
Which of the following is the correct query to get the total number of customers from the online_customer
table?
SELECT COUNT(*) AS total_customers
FROM online_customer;
You Selected
SELECT * AS total_customers
FROM online_customer;
SELECT COUNT * AS total_customers
FROM online_customer;
The COUNT() function returns the number of rows or records in the mentioned field or column. If you input a
"*" inside this function, this will count all of the rows.
Option 1 is correct, as this will return the total number of customers from the table.
Option 2 is incorrect as all the columns are selected and there is an ambiguity for alias.
Option 3 is incorrect, as COUNT() is a function and any input should be inside the parentheses.
Q No: 4
Correct Answer
Marks: 1/1
Which of the following is the correct query to get the average, maximum, and minimum
product_quantity_available from the product table?
SELECT AVG(product_quantity_avail),
MAX(product_quantity_avail),
MIN(product_quantity_avail)
FROM product;
You Selected
SELECT AVG(product_quantity_avail),
SELECT MAX(product_quantity_avail),
SELECT MIN(product_quantity_avail)
FROM product;
SELECT MEAN(product_quantity_avail),
SELECT MAX(product_quantity_avail),
SELECT MIN(product_quantity_avail)
FROM product;
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.
Option 1 is correct, as it returns the average, maximum, and minimum of product_quantity_available from the
product table.
Option 3 is incorrect; multiple SELECT statements and the MEAN() function are not valid in MySQL.
Q No: 5
Correct Answer
Marks: 1/1
Which of the following is the correct query to get the total number of unique pincodes from the address table?
SELECT COUNT(DISTINCT pincode)
FROM address;
You Selected
SELECT COUNT(*)
FROM address;
SELECT DISTINCT pincode
FROM address;
The COUNT() function is used to count the number of records of the input value.
The DISTINCT keyword is used to retrieve the values that are unique or different from any column.
The COUNT(DISTINCT) function returns the number of records that are distinct (different).
Option 1 is correct because COUNT(DISTINCT) returns the number of distinct (different) pincodes from the
address table.
Option 2 is incorrect, as COUNT(*) will return the total records of pincodes, including the duplicates as well,
from the address table.
Option 3 is incorrect, this will return all the unique pincodes instead of the count of unique records.
Q No: 6
Correct Answer
Marks: 1/1
Which of the following is the correct query to get the total quantity for each product_class_code level from the
product table?
SELECT product_class_code, SUM(product_quantity_avail)
FROM product
GROUP BY product_class_code;
You Selected
SELECT product_class_code, SUM(product_quantity_avail)
FROM product;
SELECT product_class_code, SUM(product_quantity_avail)
FROM product
GROUP BY product_class_code,SUM(product_quantity_avail);
The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), SUM(), MIN() and
AVG()) to group the result set by one or more columns.
Here, the GROUP BY statement groups rows that have the same values for product_desc and returns the
aggregate value (SUM in this case) of such a group.
Option 1 is correct, as it will fetch the sum of product_quantity_avail at the product_class_code level.
Option 3 is incorrect, as the GROUP BY clause does not support the aggregation function.
Q No: 7
Correct Answer
Marks: 1/1
Which of the following is the correct query to get the average quantity for each product_class_code level in
ascending order of average quantity from the product table?
SELECT product_class_code, AVG(product_quantity_avail) AS avg_qty
FROM product
GROUP BY product_class_code
ORDER BY avg_qty;
You Selected
SELECT product_class_code, AVG(product_quantity_avail) AS avg_qty
FROM product
GROUP BY product_class_code
ORDER BY avg_qty DESC;
SELECT product_class_code, AVG(product_quantity_avail) AS avg_qty
FROM product
ORDER BY avg_qty DESC
GROUP BY product_class_code;
SELECT product_class_code, AVG(product_quantity_avail) AS avg_qty
FROM product
GROUP BY product_class_code
ORDER BY product_class_code ASC;
The ORDER BY clause sorts the columns mentioned in the SELECT clause based on the mentioned columns.
You can use the ASC keyword for ascending order and the DESC keyword for descending order.
Option 1 is correct, as this will fetch the product_class_code level average of quantity and sort it in ascending
order of avg_qty.
Option 2 is incorrect, as this will fetch the product_class_code level average of quantity and sort it in
descending order of avg_qty.
Option 3 is incorrect, as the ORDER BY clause is invalid before the GROUP BY clause.
Option 4 is incorrect, as this will fetch the product_class_code level average of quantity and sort it in
ascending order of product_class_code instead of avg_qty.
Q No: 8
Correct Answer
Marks: 1/1
Which of the following is the correct query to get the number of orders at each payment_mode for shipper_id
50001 from the order_header table?
SELECT payment_mode, COUNT(order_id)
FROM order_header
WHERE shipper_id=50001
GROUP BY payment_mode;
You Selected
SELECT payment_mode, COUNT(order_id)
FROM order_header
GROUP BY 1
WHERE shipper_id=50001;
SELECT COUNT(order_id),payment_mode
FROM order_header
WHERE shipper_id=50001
GROUP BY COUNT(order_id);
SELECT payment_mode, COUNT(order_id)
FROM order_header
WHERE shipper_id=50001;
Option 1 is correct, as this will first filter the records for the given shipper_id and then get the number of
orders for each payment mode.
Option 2 is incorrect, as the GROUP BY clause before the WHERE clause is invalid in MySQL.
Option 3 is incorrect, as the aggregate function is invalid in the GROUP BY clause in MySQL.
Option 4 is incorrect, as the GROUP BY clause is missing when using the aggregate function.
Q No: 9
Correct Answer
Marks: 1/1
Which of the following is the correct query to get the total quantity for each product_desc having a total
The WHERE clause cannot be used with aggregate functions to filter based on the output of an aggregate
function.
The HAVING clause is used to filter the records based upon the output of the aggregate function.
Also, in the GROUP BY clause, instead of column names, you can give a column number as per the order in
the SELET statement.
Option 1 is correct because it will fetch inventory at the product_desc level and filter records with inventory
greater than 100.
Option 2 is incorrect, as the HAVING clause should be after the GROUP BY clause.
Option 3 is incorrect, as the aggregate function cannot be mentioned in the GROUP BY clause.
Option 4 is incorrect, as the WHERE clause is mentioned after the GROUP BY clause.
Q No: 10
Correct Answer
Marks: 1/1
Which of the following is the correct query to create a price_category based upon the product_price using the
The CASE expression goes through conditions and returns a value when the first condition is met (like an if-
then-else statement). So, once a condition is true, it will stop reading and return the result. If no conditions are
true, it returns the value in the ELSE clause. If there is no ELSE part and no conditions are true, it returns
NULL.
Option 2 is incorrect; for each criterion, a separate CASE clause is not required.
Option 3 is incorrect, as the CASE WHEN clause should be concluded using the END keyword.
RETAKE
Attempt History
Attempt #1
Jun 16, 11:35 PM
Evaluation Pending
Q No: 1
Find Length of Country Name
Write a SQL query to find the length of the country name in the "country" table.
Sample Output:
name_length
13
Your answer
-- Write your query below
SELECT LENGTH(name) AS name_length
FROM country;
Q No: 2
Convert Country Name to Uppercase
We have a table named country with a column named name that stores the names of different countries.
Sample Output
uppercase
UNITED STATES
Your answer
-- Write your query below
SELECT UPPER(name) AS uppercase
FROM country;
Q No: 3
Sorting Data with multiple columns
You have a table named customers with id, names, dates columns. Write a SQL query to sort the data in the
following order:
Sample Output:
id names dates
1 Jane 2022-01-01
2 John 2022-01-01
3 Alice 2022-04-01
4 Bob 2022-03-01
Your answer
-- Write your query below
SELECT id, names, dates
FROM customers
ORDER BY id ASC, names DESC, dates ASC;
Q No: 4
Custom Sorting Data with conditions
You have a table named departments with columns id and name. Write a SQL query to sort the data based on
Sample Output:
id names
2 Marketing
3 Sales
1 HR
4 Misc
Your answer
-- Write your query below
SELECT id, name
FROM departments
ORDER BY
CASE
WHEN name = 'Marketing' THEN 1
WHEN name = 'Sales' THEN 2
WHEN name = 'HR' THEN 2
ELSE 3
END,
id ASC;
We have a table named video_games that contains information about different video games. Your task is to
write a SQL query to find the unique genres in the video_games table and sort them in alphabetical order.
Output:
genre
Action-Adventure
Your answer
-- Write your query below
SELECT DISTINCT genre
FROM video_games
ORDER BY genre;
Q No: 2
Counting Individual Platforms in Video Games
Write a SQL query to count the number of individual platforms as "counts" present in
Output:
platform counts
Game Boy 2
Nintendo 3
Your answer
-- Write your query below
SELECT
platform,
COUNT(*) AS counts
FROM
video_games
GROUP BY
platform
ORDER BY
platform;
Q No: 3
Total Global Sales for Individual Platforms
We have a table named video_games that contains information about video games, including the platform
on which they were released and the global sales they have generated. Your task is to write a SQL query to
calculate the total global sales for each individual platform and round the result to 2 decimal places.
Sample Output:
platform sum_of_global_sales
Platform 1 15.75
Your answer
-- Write your query below
SELECT
platform,
ROUND(SUM(global_sales), 2) AS sum_of_global_sales
FROM
video_games
GROUP BY
platform
ORDER BY
platform;
Q No: 4
Average Global Sales by Platform
Consider a table named video_games with columns platform, version and global_sales. Write a
query to find the average global sales for each platform and round the result to 2 decimal places.
Sample Output:
platform avg_sales
Nintendo 3.83
Your answer
-- Write your query below
SELECT
platform,
ROUND(AVG(global_sales), 2) AS avg_sales
FROM
video_games
GROUP BY
platform
ORDER BY
platform;
Q No: 5
Find Maximum Global Sales for Individual Platforms
Given the video_games table, write a SQL query to find the maximum global_sales for each
individual platform.
Sample Output:
platform max_global_sales
Multiplatform 200
Your answer
-- Write your query below
SELECT
platform,
MAX(global_sales) AS max_global_sales
FROM
video_games
GROUP BY
platform
ORDER BY
platform;
Q No: 6
Minimum Global Sales by Platform
We have a table named video_games that contains information about different video games. The table has
columns for platform, versionand global_sales. Now, write a query to find the minimum global
Sample Output:
platform min_global_sales
Nintendo 110.2
Your answer
-- Write your query below
SELECT
platform,
MIN(global_sales) AS min_global_sales
FROM
video_games
GROUP BY
platform
ORDER BY
platform;
Q No: 7
Find Application Name Released in a Specific Year Range
We have a table named video_games that contains information about various video game applications. The
table has columns such as name and year_of_release. Now, write a query to retrieve the names of the
Sample Output:
name
Game B
Your answer
-- Write your query below
SELECT
name
FROM
video_games
WHERE
year_of_release BETWEEN 2019 AND 2020;
Q No: 8
Count of Null Values in Video Games Table
Write a query to find the count of null values in each column of the video_games table.
Sample Output:
Q No: 9
Sum of Critic Scores by Publisher
We have a table named video_games that contains information about video games, including the publisher and
the critic score. Some critic scores may be null. Write a query to find the sum of critic scores for each
Sample Output:
publisher avg_critic_score
Publisher X 0
Your answer
-- Write your query below
SELECT
publisher,
SUM(COALESCE(critic_score, 0)) AS avg_critic_score
FROM
video_games
GROUP BY
publisher;
Q No: 10
Top 5 Games with Highest Critic Score
Write a SQL query to display the name and publisher of the top 5 games with the highest critic score from the
video_games table.
Sample Output:
Q No: 11
Total User Count by Year of Release
We have a table named video_games that contains information about video games, including
the year_of_release and user_count columns. Your task is to write a query to find the
total user_count for each year_of_release, filtering out null values and replacing them with "unknown
year".
Sample Output:
year_of_release sum_of_user_count
2010 3000
Your answer
-- Write your query below
SELECT
COALESCE(year_of_release, 'unknown year') AS year_of_release,
SUM(user_count) AS sum_of_user_count
FROM
video_games
WHERE
user_count IS NOT NULL
GROUP BY
COALESCE(year_of_release, 'unknown year');
SELECT
IFNULL(year_of_release, 'unknown year') AS year_of_release,
SUM(user_count) AS sum_of_user_count
FROM
video_games
WHERE
user_count IS NOT NULL
GROUP BY
IFNULL(year_of_release, 'unknown year');
Dear Participants,
(one invoice per service provided). Multiple vendors provide services in a city.
Attempt History
Attempt #1
Jun 23, 3:47 PM
Marks: 10
Q No: 1
Correct Answer
Marks: 1/1
Correct Answer
Marks: 1/1
Payment due_date for an invoice is on 30th days from the invoice_date. Which of the following function
calculates payment due date
DATE_ADD(invoice_date, INTERVAL 30 day)
You Selected
DATE_ADD(invoice_date, INTERVAL 1 month)
Both the functions will always give same result hence any of these two function can be used
Not possible to calculate payment due date in SQL, need to write a separate program
Q No: 3
Correct Answer
Marks: 1/1
If invoice_date is ‘2022-06-01’ then what value will be retrieved by the following query executed on 2nd June
2022
2
1
You Selected
0
-1
Q No: 4
Correct Answer
Marks: 1/1
If invoice_amount = 1270.51 then both the functions ROUND(invoice_amount, 0) and
TRUNCATE(invoice_amount, 0) will return the same value
True
False
You Selected
Q No: 5
Correct Answer
Marks: 1/1
If customer_name contains value “David Smith” then use of function LTRIM(RTRIM(customer_name)) in
the SELECT clause will display “DavidSmith”
True
False
You Selected
Q No: 6
Correct Answer
Marks: 1/1
What will be the output of SUBSTR(customer_name, INSTR(customer_name, '.')+1 ) in SELECT
clause if customer_name contains value “Mr.David”.
Mr.
.David
David
You Selected
Mr.David
Q No: 7
Correct Answer
Marks: 1/1
Following query will display one row per vendor who has provided service in city of London and compute
FROM invoice
ORDER BY vendor_id;
True
False
You Selected
Q No: 8
Correct Answer
Marks: 1/1
Following query will retrieve vendors who have average invoice amount < 10000
FROM invoice
GROUP BY vendor_id;
True
False
You Selected
Q No: 9
Correct Answer
Marks: 1/1
What will be the output of the query given below? Choose the most appropriate answer
FROM invoice
GROUP BY service_city;
Correct Answer
Marks: 1/1
Which of the following statement is correct in the context of query which consists of SELECT, FROM,
WHERE, GROUP BY and HAVING clause?
WHERE clause is applied to filter the rows before applying the GROUP BY clause
You Selected
HAVING clause is applied to filter the rows before applying the GROUP BY clause
Either WHERE or HAVING clause is applied first depending on which clause consists of aggregate function
A SQL statement cannot contain both WHERE and HAVING clauses
Joining Data Quick Practice Quiz
Type
:
Practice Quiz
Questions
:
5
Scoring Policy
:
Highest Score
Your Marks
:
4/5
RETAKE
Attempt History
Attempt #1
Jun 23, 7:35 AM
Marks: 4
Q No: 1
Correct Answer
Marks: 1/1
Which of the following is the correct query to get the address details (city, state, and
hint: The address table has address details, and the online_customer table has
customer details.
You Selected
SELECT * ,oc.*
FROM online_customer as oc
LEFT JOIN address ad
ON oc.address_id=ad.address_id;
SELECT *, ad.*
FROM online_customer as oc
LEFT JOIN address ad
ON oc.address_id=ad.address_id;
SELECT oc.*, ad.*
FROM online_customer as oc
LEFT JOIN address ad
ON oc.address_id=ad.address_id;
LEFT JOIN returns all rows from the left table as well as the rows that match from the
right table. If there is no match, the result is NULL from the right side. The ON keyword
is used to specify the keys (columns) from the tables on which they are to be joined.
Aliases to the tables help us specify tables in the complex queries.
Option 1 is correct, as it will map all the records of the online customer table to the
corresponding address table using LEFT JOIN and fetch all the columns of the left
table and address details of the address table.
Option 2 is incorrect, as it will map all the records of the online customer table to the
corresponding address table using LEFT JOIN. The use of the asterisk () in the
SELECT statement is not valid because it is not clear which table the asterisk is
intended to select columns from. Additionally, the use of the asterisk followed by the
columns of "oc." is not valid as it specifies to include all columns twice in the result.
Option 3 is incorrect, as it will map all the records of the online customer table to the
corresponding address table using LEFT JOIN. The use of the asterisk () in the
SELECT statement is not valid because it is not clear which table the asterisk is
intended to select columns from. Additionally, the use of the asterisk followed by the
columns of "ad." is not valid as it specifies to include all columns twice in the result.
Option 4 is incorrect, as it will map all the records of the online customer table to the
corresponding address table using LEFT JOIN and fetch all the columns from both
tables that are not required in the output.
.
Q No: 2
Incorrect Answer
Marks: 0/1
Which of the following is the correct query to get the customer details for all the
addresses in the address table?
SELECT ad.*,oc.*
FROM address ad
LEFT JOIN online_customer as oc
ON oc.address_id=ad.address_id;
You Selected
SELECT oc.*,ad.*
FROM online_customer as oc
RIGHT JOIN address ad
ON oc.address_id=ad.address_id;
Correct Option
SELECT ad.*,oc.*
FROM online_customer as oc
LEFT JOIN address ad
ON oc.address_id=ad.address_id;
SELECT oc.*,ad.*
FROM address ad
RIGHT JOIN online_customer as oc
ON oc.address_id=ad.address_id;
You Selected
LEFT JOIN returns all rows from the left table as well as the rows that match from the
right table. If there is no match, the result is NULL on the right side.
RIGHT JOIN returns all rows from the right table as well as the rows that match from the
left. If there is no match, the result is NULL on the left side.
A RIGHT JOIN can be done using the LEFT JOIN by changing the order of the tables.
Option 1 is correct, as it will map all the addresses in the address table to the
corresponding online_customer using a LEFT JOIN and fetch all the records from the
address table.
Option 2 is correct, as it will map all the customers in online_customer to the
corresponding addresses in the address table using a RIGHT JOIN and fetch all the
records from the address table.
Option 3 is incorrect, as it will map all the customers in online_customer to the
corresponding addresses in the address table using a LEFT JOIN and fetch all the
records from the online customer table that are not required.
Option 4 is incorrect, as it will map all the addresses in the address table to the
corresponding customers in the online_customer table using a RIGHT JOIN and fetch
all the records from the online customer table that are not required.
Q No: 3
Correct Answer
Marks: 1/1
Which of the following is the correct query to get the sum of inventory available at
product_class_desc level?
SELECT product_class_desc, SUM(product_quantity_avail)
FROM product_class AS pc
LEFT JOIN product AS p
ON pc.product_class_code=p.product_class_code
GROUP BY 1
ORDER BY 1 ;
You Selected
SELECT product_class_desc, SUM(product_quantity_avail)
FROM product_class
LEFT JOIN product AS p
ON pc.product_class_code=p.product_class_code
GROUP BY 1
ORDER BY 1 ;
SELECT product_class_desc, COUNT(product_quantity_avail)
FROM product_class AS pc
LEFT JOIN product AS p
ON pc.product_class_code=p.product_class_code
GROUP BY 1
ORDER BY 1 ;
Option 1 is correct, as this joins the "product_class" table and "product" table on the
"product_class_code" column, using a LEFT JOIN. The results are grouped by the
"product_class_desc" column, and the sum of "product_quantity_avail" is calculated for
each group. Finally, the results are ordered by the "product_class_desc" column.
Option 2 is incorrect, as this will not join the tables due to incorrect column mentioned
in the ON clause.
Option 3 is incorrect, as this joins the "product_class" table and "product" table on the
"product_class_code" column, using a LEFT JOIN. The results are grouped by the
"product_class_desc" column, and count of "product_quantity_avail" is calculated for
each group. Finally, the results are ordered by the "product_class_desc" column.
Q No: 4
Correct Answer
Marks: 1/1
Which of the following is the correct query to get the shipper and payment details for the
ordered items?
SELECT oi.*, oh.payment_mode, oh.shipper_id
FROM order_items as oi
INNER JOIN order_header as oh
ON oi.order_id=oh.order_id;
You Selected
SELECT oh.*, oi.payment_mode, oi.shipper_id
FROM order_items as oi
INNER JOIN order_header as oh
ON oi.order_id=oh.order_id;
SELECT oi.*, oi.payment_mode, oh.shipper_id
FROM order_items as oi
INNER JOIN order_header as oh
ON oi.order_id=oh.order_id;
The INNER JOIN only returns rows where there is a matching row in both tables.
Option 1 is correct, as it performs an INNER JOIN between two tables: "order_items"
(aliased as "oi") and "order_header" (aliased as "oh") based on the matching of
"oi.order_id" and "oh.order_id". The SELECT clause will fetch all columns from "oi" and
the "payment_mode" and "shipper_id" columns from "oh".
Option 2 is incorrect, as it performs an INNER JOIN between two tables: "order_items"
(aliased as "oi") and "order_header" (aliased as "oh") based on the matching of
"oi.order_id" and "oh.order_id". But the columns specified in the SELECT clause are
invalid.
Option 3 is incorrect, as it performs an INNER JOIN between two tables: "order_items"
(aliased as "oi") and "order_header" (aliased as "oh") based on the matching of
"oi.order_id" and "oh.order_id". But the columns specified in the SELECT clause are
invalid.
Q No: 5
Correct Answer
Marks: 1/1
Which of the following is the correct query to get the total product quantity ordered and
number of products in the order at the shipper and payment mode level?
SELECT oh.shipper_id, oh.payment_mode, SUM(oi.product_quantity),
COUNT(oi.product_id)
FROM order_items as oi
INNER JOIN order_header as oh
ON oi.order_id=oh.order_id
GROUP BY 1, 2
ORDER BY 1,2;
You Selected
SELECT oh.shipper_id, oi.payment_mode, SUM(oh.product_quantity),
COUNT(oi.product_id)
FROM order_items as oi
INNER JOIN order_header as oh
ON oi.order_id=oh.order_id
GROUP BY 1, 2
ORDER BY 1,2;
SELECT shipper_id, oh.payment_mode, SUM(oh.product_quantity),
COUNT(oh.product_id)
FROM order_items as oi
INNER JOIN order_header as oh
ON oi.order_id=oh.order_id
GROUP BY 1, 2
ORDER BY 1,2;
Option 1 is correct, as it joins the "order_items" table and "order_header" table on the
"order_id" column and groups the results by the "shipper_id" and "payment_mode"
columns. It calculates the sum of "product_quantity" and the count of "product_id" for
each group. Finally, the results are ordered by the "shipper_id" and "payment_mode"
columns.
Option 2 is incorrect, as it joins the "order_items" table and "order_header" table on the
"order_id" column. This is will be not able to group the results by the "shipper_id" and
"payment_mode" columns as these columns are not specified from the correct table.
Option 3 is incorrect, as it joins the "order_items" table and "order_header" table on the
"order_id" column. This is will be not able to group the results by the "shipper_id" and
"payment_mode" columns as these columns are not specified from the correct table.
Correct Answer
Marks: 1/1
Which of the following is not true?
Left Join fetches all the records from the left table and only matching records from the right
table.
Right Join fetches all the records from the right table and only matching records from the left
table.
Self Join can be used to join a table with itself.
Cross Join fetches all the matching records from the two tables if no where clause is used.
You Selected
Q No: 2
Correct Answer
Marks: 1/1
Consider the table and the query of the table below
EMPLOYEES
NAM
EID MID
E
1 TOM 4
2 BILL 3
3 JIM 4
4 JACK 5
SELECT DISTINCT
e1.name,
e2.name
FROM
employees e1
CROSS JOIN
employees e2
WHERE
e1.mid = e2.eid;
The query above returns how many rows as the output result set?
16
4
3
You Selected
2
A CROSS JOIN without a where condition returns the cartesian product of the rows of
the table. But when a WHERE clause is present, the number of rows in the output result
set changes based on the condition.
The query above tries to fetch the employee name and their respective managers by
matching the manager id(MID) with the employee id (EID).
Correct Answer
Marks: 1/1
NAME MARKS
Sam 60
Jack 40
Jim 85
Henry 60
What is the output of the below query for the above table STUDENTS?
SELECT
NAME,
MARKS,
FROM STUDENTS;
RANK and DENSE_RANK assign ranks to the data based on the column value.
RANK assigns the same rank number if it finds two equal values and skips the next integer for
the next value, while DENSE_RANK assigns the same rank for two equal values but does not
skip the next rank for the next value.
In the above table, 2 students have each got 60 marks. RANK() assigns number 2 to both the
students, and for the next student with 40 marks, it assigns 4 and not 3. DENSE_RANK() also
assigns rank 2 to both the students who scored 60 marks and assigns a rank of 3 to the next
student who scored 40 marks.
Q No: 2
Correct Answer
Marks: 1/1
What is the output of the below query for the above table (DATA)?
SELECT
DEPT,
NAME,
MARKS,
FROM DATA
DEP
NAME MARKS AVERAGE_MARKS
T
1 Sam 60 67.5000
1 Jack 40 67.5000
2 Jim 75 50.0000
2 Henry 60 50.0000
DEP
NAME MARKS AVERAGE_MARKS
T
1 Sam 60 50.0000
1 Jack 40
2 Jim 75 67.5000
2 Henry 60
AVG() function is used without the HAVING clause, so the query is erroneous.
DEP
NAME MARKS AVERAGE_MARKS
T
1 Sam 60 50.0000
1 Jack 40 50.0000
2 Jim 75 67.5000
2 Henry 60 67.5000
You Selected
AVG() function can also be used with a partition by clause to perform the calculations. The
above query calculates the average marks of students in each department and assigns the
calculated value to each record in the table.
Attempt History
Attempt #1
Jun 25, 3:54 AM
Marks: 5
Q No: 1
Correct Answer
Marks: 1/1
Which of the following is the correct query to get the product class code, product ID, product description, and
total number of products associated with each product class code?
SELECT
product_class_code,
product_id,
product_desc,
COUNT(*) OVER(PARTITION BY product_class_code) AS no_of_product
FROM product;
You Selected
SELECT
product_class_code,
product_id,
product_desc,
COUNT(*) AS no_of_product
FROM product;
SELECT
product_class_code,
product_id,
product_desc,
SUM(*) OVER(PARTITION BY product_class_code) AS no_of_product
FROM product;
Query 1 is correct:
The "PARTITION BY" clause groups the rows of the table by the "product_class_code" column, so
the COUNT(*) function counts the number of rows/products in each group. The result of
the COUNT() function is aliased as "no_of_product". In this case, the count of rows will be
calculated separately for each unique "product_class_code".
Query 2 is incorrect:
The above query is incorrect because it is just counting the number of rows. We are not giving any
condition to count the number of products/rows for each product class code.
Query 3 is incorrect:
The "SUM()" expression is not valid. The "SUM" function is used to add up the values in a column,
but in this case, we are trying to sum the asterisk (*). SUM() function needs a column name for which
MYSQL can add a value. For this question, we need to COUNT() function instead of
using SUM() function as we are calculating the no of products by counting them.
Q No: 2
Correct Answer
Marks: 1/1
Which of the following is the correct query to get the product class code, product id, product description,
product quantity for each product, and total product quantity available for each product class code?
SELECT
product_class_code,
product_id,
product_desc,
product_quantity_avail,
SUM(product_quantity_avail) OVER(PARTITION BY PRODUCT_CLASS_CODE) AS
total_product_quantity_available
FROM product;
You Selected
SELECT
product_class_code,
product_id,
product_desc,
product_quantity_avail,
SUM(*) OVER(PARTITION BY PRODUCT_CLASS_CODE) AS
total_product_quantity_available
FROM product;
SELECT
product_class_code,
product_id,
product_desc,
product_quantity_avail,
SUM(product_quantity_avail) OVER(PARTITION BY product_id) AS
total_product_quantity_available
FROM product;
Query 1 is correct:
Query 2 is incorrect:
The "SUM" function is used to add up the values in a column, but in this case, you are trying to sum
the asterisk (*), which is not valid in SQL. To fix the error, you need to specify a valid column name
that you want to sum instead of the asterisk. For example, if you want to sum the number of products
for each "product_class_code", you can use "SUM(product_quantity_avail)" to sum the available
quantity of products for each product class code.
Query 3 is incorrect:
In this query, we are grouping the rows based on the product id (as mentioned in PARTITION
BY), which is incorrect as we need to find the sum of product quantities available for each product
class code. We need to be careful while assigning the column in the window function, as MySQL will
behave according to the query provided and will provide you with the result, which may be incorrect
and not desirable.
Q No: 3
Correct Answer
Marks: 1/1
SELECT
product_class_code,
product_id,
product_desc,
product_price,
AVG(product_price) OVER(PARTITION BY product_class_code) AS
avg_product_price,
CASE
WHEN product_price > AVG(product_price) OVER(PARTITION BY product_class_code)
THEN 'above average'
ELSE 'below average'
END AS price_flag
FROM product;
SELECT
product_class_code,
product_id,
product_desc,
product_price,
AVG(product_price),
CASE
WHEN product_price > AVG(product_price) THEN 'above average'
ELSE 'below average'
END AS price_flag
FROM product
GROUP BY 1;
True
False
You Selected
Query 1:
Query 2:
The "GROUP BY 1" clause groups the rows by the "product_class_code" column. In other words,
all rows with the same "product_class_code" value will be grouped together, and an average of
the "product_price" column will be calculated for each group.
The result set will contain 6 columns: "product_class_code", "product_id", "product_desc",
"product_price", "AVG(product_price)", and "price_flag". This query will fetch fewer rows as
compared to the other query because, in the first query, we are getting the product class code along
with all products associated with it, but in this query, we are only getting one row for each product
class code as we are grouping the rows based on the product class code.
Q No: 4
Correct Answer
Marks: 1/1
Which of the following is the correct query to get the rank and dense rank of the product class code based on
the total product quantity available for each product class code?
SELECT
PRODUCT_CLASS_CODE,
SUM(product_quantity_avail) as total_quantity_avail,
DENSE_RANK() OVER (ORDER BY total_quantity_avail DESC) as dense_ranking,
RANK() OVER (ORDER BY total_quantity_avail DESC) as ranking
FROM product;
SELECT
product_class_code,
SUM(product_quantity_avail) as total_quantity_avail,
DENSE_RANK() OVER (ORDER BY SUM(product_quantity_avail) DESC) as
dense_ranking,
RANK() OVER (ORDER BY SUM(product_quantity_avail) DESC) as ranking
FROM product
GROUP BY product_class_code;
You Selected
SELECT
PRODUCT_CLASS_CODE,
SUM(product_quantity_avail) as total_quantity_avail,
DENSE_RANK() OVER (ORDER BY SUM(product_quantity_avail) DESC) as
dense_ranking,
RANK() OVER (ORDER BY SUM(product_quantity_avail) DESC) as ranking
FROM product;
Query 2 is correct:
Query 1 is incorrect:
Query 3 is incorrect:
This query will not provide the desired result as this query will sum the product quantities available
without grouping for each product class code because the GROUP BY clause is missing. As a result,
the query will fetch only one row with the sum of all the product quantity available without
considering the product class code, and as a result, the dense rank and rank value with a value of 1
will get generated.
Q No: 5
Correct Answer
Marks: 1/1
Which of the following is the correct query to get the product class code, product id, product description, and
product price and divide the group into five based on the product price?
SELECT
product_class_code,
product_id,
product_desc,
product_price,
NTILE(5) OVER (ORDER BY product_price) AS ntile_group
FROM product;
You Selected
SELECT
product_class_code,
product_id,
product_desc,
product_price,
NTILE(5) OVER (PARTITION BY product_class_code ORDER BY product_price) AS
ntile_group
FROM product;
SELECT
product_class_code,
product_id,
product_desc,
product_price,
NTILE(5) OVER (product_price) AS ntile_group
FROM product;
Query 1 is correct:
Result of the NTILE function, which is an aggregate function used to divide a set of rows into a
specified number of groups (in this case, 5) based on the value of product_price, with the groups
being ordered in ascending order.
The purpose of the NTILE function is to divide the rows into groups of roughly equal size based on
the value of product_price. The result of the function is assigned to a new column
named "ntile_group".
Query 2 is incorrect:
This SQL query is similar to the previous query, but with one additional clause in
the NTILE function. The NTILE function now includes a PARTITION BY clause, which means
that the rows are divided into groups of roughly equal size based on the value of product_price, but
within each separate partition of product_class_code.
The groups within each partition are ordered in ascending order based on the value of product_price.
Query 3 is incorrect:
The query you provided has a syntax error in the NTILE function. The NTILE function requires
an OVER clause to define the set of rows that the function operates on, and the OVER clause must
include an ORDER BY clause to specify the ordering of the rows for grouping.
Attempt History
Attempt #1
Jun 25, 5:21 AM
Marks: 5
Q No: 1
Correct Answer
Marks: 1/1
Which of the following is the correct query to get the product details (product_id and product_desc) for the
products having prices higher than the average price from the product table?
SELECT
PRODUCT_ID,
PRODUCT_DESC
FROM PRODUCT
WHERE PRODUCT_PRICE > (SELECT
AVG(PRODUCT_PRICE)
FROM PRODUCT);
You Selected
SELECT
PRODUCT_ID,
PRODUCT_DESC
FROM PRODUCT
WHERE PRODUCT_PRICE < (SELECT
AVG(PRODUCT_PRICE)
FROM PRODUCT);
SELECT
PRODUCT_ID,
PRODUCT_DESC
FROM PRODUCT
WHERE PRODUCT_PRICE > SELECT
MIN(PRODUCT_PRICE)
FROM PRODUCT;
Option 1 is correct, as the inner query will return the average price of all the products in the product
table, and then the outer query will filter for the records that have a product_price greater than the
average price and fetch the product_id and product_desc.
Option 2 is incorrect, as the inner query will return the average price for all the products in the
product table, and the outer query will filter for the records that have a product_price less than the
average price and fetch the product_id and product_desc.
Option 3 is incorrect, as the inner query will return the minimum price for all the products in the
product table, and the outer query will filter for the records that have a product_price greater than the
average price and fetch the product_id and product_desc.Also, the condition in the WHERE clause is
not enclosed in parentheses. Parentheses are mandatory in SQL when writing a subquery.
Q No: 2
Correct Answer
Marks: 1/1
Which of the following is the correct query to get the order_id from the order_items table for the products
having a product_price greater than $8500?
SELECT
ORDER_ID
FROM ORDER_ITEMS
WHERE PRODUCT_ID = (SELECT
PRODUCT_ID
FROM PRODUCT
WHERE PRODUCT_PRICE > 8500);
SELECT
ORDER_ID
FROM ORDER_ITEMS
WHERE PRODUCT_ID NOT IN (SELECT
PRODUCT_ID
FROM PRODUCT
WHERE PRODUCT_PRICE > 8500);
SELECT
ORDER_ID
FROM ORDER_ITEMS
WHERE PRODUCT_ID IN (SELECT
PRODUCT_ID
FROM PRODUCT
WHERE PRODUCT_PRICE > 8500);
You Selected
Option 1 is incorrect, as the inner query will filter the product_ids having a value greater than $8500 from
the product table and return a list of product_ids. The outer query will not filter for the records because, in
the WHERE clause, the "=" operator is not valid for a list.
Option 2 is incorrect, as the inner query will filter the product_ids having a value greater than $8500 from
the product table and return a list of product_ids. The outer query will filter for the records that are not
present in the list from the order_items table using the WHERE and IN clauses and fetch the order_ids.
Option 3 is correct, as the inner query will filter the product_ids having a value greater than $8500 from
the product table and return a list of product_ids. The outer query will filter for the records present in the list
from the order_items table using the WHERE and IN clauses and fetch the order_ids.
Q No: 3
Correct Answer
Marks: 1/1
Which of the following is the correct query to get the average price at product_class_code for products having
a product_price greater than the average product_price?
SELECT PRODUCT_CLASS_CODE, AVG(PRODUCT_PRICE) AS AVG_PRICE
FROM PRODUCT
WHERE PRODUCT_PRICE > (
SELECT AVG(PRODUCT_PRICE)
FROM PRODUCT)
GROUP BY PRODUCT_CLASS_CODE;
You Selected
SELECT PRODUCT_CLASS_CODE, PRODUCT_PRICE
FROM PRODUCT
WHERE PRODUCT_PRICE > (
SELECT AVG(PRODUCT_PRICE)
FROM PRODUCT)
GROUP BY PRODUCT_CLASS_CODE;
SELECT PRODUCT_CLASS_CODE, AVG(PRODUCT_PRICE) AS AVG_PRICE
FROM PRODUCT
WHERE PRODUCT_PRICE < (
SELECT AVG(PRODUCT_PRICE)
FROM PRODUCT)
GROUP BY PRODUCT_CLASS_CODE;
Option 1 is correct, as the inner query will return the average price of all products. The outer query will filter
the products having price greater than average price. Then it will calculate the average price at
product_class_code level for the filtered records.
option 2 is incorrect, as the inner query will return the average price of all products. The outer query will filter
the products having price greater than average price. Then it will fetch the product_class_code and
product_price that is not required output.
Option 3 is incorrect, as the inner query will return the average price of all products. The outer query will filter
the products having price less than average price. Then it will calculate the average price at
product_class_code level for the filtered records.
Q No: 4
Correct Answer
Marks: 1/1
Which of the following is the correct query to get the product details (product_id and product_price) for the
products having prices ranged between average price and 50000 from the product table?
SELECT
PRODUCT_ID,
PRODUCT_PRICE
FROM PRODUCT
WHERE PRODUCT_PRICE = (
SELECT AVG(PRODUCT_PRICE)
FROM PRODUCT
)
AND 50000
ORDER BY PRODUCT_PRICE;
SELECT
PRODUCT_ID,
AVG(PRODUCT_PRICE) AS AVERAGE_PRICE
FROM PRODUCT
WHERE PRODUCT_PRICE BETWEEN (
SELECT PRODUCT_PRICE
FROM PRODUCT
)
AND 50000
ORDER BY PRODUCT_PRICE;
SELECT
PRODUCT_ID,
PRODUCT_PRICE
FROM PRODUCT
WHERE PRODUCT_PRICE BETWEEN (
SELECT AVG(PRODUCT_PRICE)
FROM PRODUCT
)
AND 50000
ORDER BY PRODUCT_PRICE;
You Selected
Option 1 is incorrect, as the inner query will return the average product price from the product table. The outer
query will not filter for the records because, in the WHERE clause the "=" operator and AND condition is not
valid.
Option 2 is incorrect, as the inner query will return all the values of product price from the product table. The
outer query will not filter the records as the inner query is providing a list that is not valid in the BETWEEN
AND clause.
Option 3 is correct, as the inner query will return the average product price from the product table. The outer
query will filter the records that are priced between average price and $50,000 and then fetch the product_id
and product_price for the filtered records.
Q No: 5
Correct Answer
Marks: 1/1
Write a query to get the number of products that falls under each price bracket.
SELECT
PRICE_BRACKET,
COUNT(PRICE_BRACKET) AS COUNT_OF_PRODUCTS
FROM PRODUCT
WHERE PRICE_BRACKET IN (
SELECT PRODUCT_ID,
PRODUCT_PRICE,
CASE
WHEN PRODUCT_PRICE >= 10000 THEN 'TIER 1'
WHEN PRODUCT_PRICE BETWEEN 5500 AND 10000 THEN 'TIER 2'
WHEN PRODUCT_PRICE <= 5500 THEN 'TIER 3'
ELSE PRODUCT_PRICE
END AS PRICE_BRACKET
FROM PRODUCT
) AS P
GROUP BY PRICE_BRACKET;
SELECT
PRICE_BRACKET,
COUNT(PRICE_BRACKET) AS COUNT_OF_PRODUCTS
FROM (
SELECT PRODUCT_ID,
PRODUCT_PRICE,
CASE
WHEN PRODUCT_PRICE >= 10000 THEN 'TIER 1'
WHEN PRODUCT_PRICE BETWEEN 5500 AND 10000 THEN 'TIER 2'
WHEN PRODUCT_PRICE <= 5500 THEN 'TIER 3'
ELSE PRODUCT_PRICE
END AS PRICE_BRACKET
FROM PRODUCT
) AS P
GROUP BY PRICE_BRACKET;
You Selected
SELECT
PRICE_BRACKET,
PRICE_BRACKET
FROM (
SELECT PRODUCT_ID,
PRODUCT_PRICE,
CASE
WHEN PRODUCT_PRICE >= 10000 THEN 'TIER 1'
WHEN PRODUCT_PRICE BETWEEN 5500 AND 10000 THEN 'TIER 2'
WHEN PRODUCT_PRICE <= 5500 THEN 'TIER 3'
ELSE PRODUCT_PRICE
END AS PRICE_BRACKET
FROM PRODUCT
) AS P
GROUP BY PRICE_BRACKET;
Option 1 is incorrect, as the inner query classifies the price_bracket based upon the criteria and fetches the
product ID, product price, and price bracket and stores it as a table "P." The outer query will not fetch the
records as the WHERE clause is not valid with an entire table using an IN statement.
Option 2 is correct, as the inner query classifies the price_bracket based upon the criteria and fetches the
product ID, product price, and price bracket and stores it as a table "P." The outer query gets the number of
records for each price_bracket from the table P.
Option 3 is incorrect, as the inner query classifies the price_bracket based upon the criteria and fetches the
product ID, product price, and price bracket and stores it as a table "P." The outer query doesn't aggregate at
the price_bracket level.
Correct Answer
Marks: 1/1
The Head of the Department of Electronics in a college wants to see a list of students who secured less than the
average marks of all the students. The data pertaining to the same is present in the STUDENT table. Which of
the following query(s) can be used to accomplish the task?
SELECT NAME FROM STUDENT WHERE MARKS < (SELECT AVG(MARKS) FROM STUDENT);
You Selected
SELECT NAME FROM STUDENT WHERE MARKS < AVG(MARKS);
SELECT NAME FROM STUDENT HAVING MARKS < (SELECT AVG(MARKS) FROM STUDENT);
SELECT NAME FROM STUDENT WHERE MARKS IN (SELECT AVG(MARKS) IN STUDENT);
The inner query fetches the average marks of the students. The outer query retrieves all the names of students
whose marks are less than the value returned by the inner query.
Q No: 2
Correct Answer
Marks: 1/1
SELECT MAX(salary)
FROM Employee
The subquery -
SELECT MAX(salary)
FROM Employee
WHERE Salary NOT IN (SELECT Max(Salary) FROM Employee);
runs first. This will return the maximum salary and the WHERE condition, excluding it. Next, the SELECT
statement runs, which will fetch the MAX of the remaining salary numbers, giving you the 2nd highest as the
first was excluded.
Practice Exercise 4 with AI Mentor
Type
:
Practice Quiz
Questions
:
7
Scoring Policy
:
Latest Score
Your Marks
:
6/7
Instructions
RETAKE
Attempt History
Attempt #1
Jun 25, 6:23 PM
Marks: 6
Q No: 1
Marks: 1/1
Retrieve Employees with Above Average Salary
and SALARY. Write a query to retrieve the EMPLOYEE_ID, FIRST_NAME, LAST_NAME, and SALARY details
of those employees whose salary is greater than the average salary of all the employees.
Sample Output:
Q No: 2
Marks: 1/1
Display Employee Details by Department Location
We have two tables: employee_details and department_details. The employee_details table contains
information about employees, including their first name, last name, and department ID. The department_details
table contains information about departments, including their department ID and location ID.
Write a query to display the first name, last name, and department ID of employees whose department is
Sample Output:
Q No: 3
Marks: 1/1
Extract Employee Details from Specific Departments
We have two tables: 'employee_details' and 'department_details'. The 'employee_details' table contains
information about employees, including their employee_id, first_name, last_name, job_id, and department_id.
The 'department_details' table contains information about departments, including their department_id and
department_name.
Now, write a query to extract the employee_id, first_name, last_name, job_id, and department_id of employees
who work in any of the departments with the names 'Shipping', 'Executive', or 'Finance'.
Sample Output:
DEPARTMENT_I
EMPLOYEE_ID FIRST_NAME LAST_NAME JOB_ID D
105 Nancy Khoo ST_CLERK 70
Your answer
-- Write your query below
SELECT
e.employee_id,
e.first_name,
e.last_name,
e.job_id,
e.department_id
FROM
employee_details e
WHERE
e.department_id IN (
SELECT
d.department_id
FROM
department_details d
WHERE
d.department_name IN ('Shipping', 'Executive', 'Finance')
);
Q No: 4
Marks: 1/1
Extract employee details from Contracting department
contains information about employees, including their employee_id, first_name, last_name, phone_number,
salary, job_id, and department_id. The department_details table contains information about
Now write a query to extract the employee_id, first_name, last_name, phone_number, salary, and job_id of the
Sample Output:
Q No: 5
Marks: 1/1
Extracting Employee Details
contains information about employees, including their ID, first name, last name, phone number, salary, and job
ID. The department_details table contains information about departments, including their ID and name.
Now, write a query to extract the employee ID, first name, last name, phone number, salary, and job ID of
Sample Output:
Q No: 6
Marks: 0/1
Display First Hired Employees
We have a table named employee_details which contains information about employees, including
query to display the EMPLOYEE_ID, FIRST_NAME, LAST_NAME, JOB_ID, and DEPARTMENT_ID of the
employees who were recruited first in their respective departments for each job position.
Sample Output:
DEPARTMENT_I
EMPLOYEE_ID FIRST_NAME LAST_NAME JOB_ID D
103 Bruce Popp FI_MGR 60
Your answer
-- Write your query below
SELECT
EMPLOYEE_ID,
FIRST_NAME,
LAST_NAME,
JOB_ID,
DEPARTMENT_ID
FROM (
SELECT
EMPLOYEE_ID,
FIRST_NAME,
LAST_NAME,
JOB_ID,
DEPARTMENT_ID,
HIRE_DATE,
FIRST_VALUE(EMPLOYEE_ID) OVER (
PARTITION BY DEPARTMENT_ID, JOB_ID
ORDER BY HIRE_DATE
) AS FIRST_EMPLOYEE_ID
FROM
employee_details
) subquery
WHERE
EMPLOYEE_ID = FIRST_EMPLOYEE_ID;
Q No: 7
Marks: 1/1
Display Employees with Maximum Salary for Each Job
columns EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY, and JOB_ID. Write a query to display
the EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY, and JOB_ID of the employees who earn the
Sample Output:
Attempt History
Attempt #1
Jun 30, 3:06 PM
Marks: 10
Q No: 1
Correct Answer
Marks: 1/1
Which of the following is not a type of join
Self join
Union join
You Selected
Left Outer join
Inner join
Q No: 2
Correct Answer
Marks: 1/1
INNER JOIN between two tables returns ‘m’ number of rows. LEFT OUTER JOIN between the same two
tables returns ‘n’ number of rows. In both the queries, no other criteria (other than join condition) are used.
Which of the following statement is TRUE?
m <= n
You Selected
n <= m
n<m
m will always be equal to n, irrespective of the data in the tables
Q No: 3
Correct Answer
Marks: 1/1
Which of the following is correct syntax for LEFT OUTER join
SELECT * FROM cities c LEFT OUTER JOIN invoices i ON c.city_code = i.service_city_code;
You Selected
SELECT * FROM cities c OUTER JOIN invoices i ON c.city_code = i.service_city_code;
SELECT * FROM cities c, invoices i WHERE c.city_code LEFT OUTER = i.service_city_code;
All the listed queries have correct syntax
Q No: 4
Correct Answer
Marks: 1/1
Correct Answer
Marks: 1/1
The following diagram is representation of
Inner Join
Left Outer Join
Right Outer Join
You Selected
Left Inner Join
Q No: 6
Correct Answer
Marks: 1/1
A stakeholder wants a list of vendors who have not raised any invoices. Which of the following query will
meet the requirement?
SELECT v.* FROM vendors v WHERE v.vendor_id IN (SELECT DISTINCT(i.vendor_id) FROM invoices
i);
SELECT v.* FROM vendors v WHERE v.vendor_id IN (SELECT DISTINCT(i.vendor_id) FROM invoices i
WHERE i.vendor_id = NULL);
SELECT v.* FROM vendors v INNER JOIN invoices i ON v.vendor_id = i.vendor_id;
SELECT v.* FROM vendors v WHERE v.vendor_id NOT IN (SELECT DISTINCT(i.vendor_id) FROM
invoices i);
You Selected
Q No: 7
Correct Answer
Marks: 1/1
In the context of subquery, which of the following is a possibility
First outer query is executed and once it is complete then inner query is executed
Inner query is executed first
You Selected
Subqueries do not depend on each other so either Outer or Inner query can be executed first and then execute
the other
A good coding practice is to avoid subqueries
Q No: 8
Correct Answer
Marks: 1/1
SELECT c.*, i.* FROM cities c LEFT JOIN invoices i ON c.city_code = i.service_city_code;
What can be inferred from this?
Correct Answer
Marks: 1/1
The LEAD() and LAG() functions in MySQL are used to get the succeeding and preceding values of any row
within its partition respectively. State True or False.
True
You Selected
False
Q No: 10
Correct Answer
Marks: 1/1
Which of the following is true about Analytical/Window functions in MySQL?
The window function uses the OVER() clause to calculate the aggregate result on a group of rows based on the
candidate key.
We cannot implement window functions without using the OVER() clause.
The aggregate result produced from a group of rows is shared for each row in the group.
All of the above
You Selected