Exit Chapter 07 · 🧩 Querying Using SQL — Quiz

Playing as a guest? to earn XP and track your progress.

Question 1 of 50 0 correct so far

🧩 Querying Using SQL Quiz

Q1.

Which clause is used to sort the result of a query in a specific order?

Explanation

The ORDER BY clause is used to sort the result set in ascending or descending order.

Q2.

By default, the ORDER BY clause sorts data in which order?

Explanation

If no modifier (ASC/DESC) is specified, ORDER BY defaults to Ascending (ASC) order.

Q3.

Which aggregate function is used to count the number of rows including NULL values?

Explanation

COUNT(*) counts all rows in the table regardless of NULL values. COUNT(column) ignores NULLs.

Q4.

What is the purpose of the DISTINCT keyword when used with an aggregate function?

Explanation

DISTINCT eliminates duplicate values. For example, COUNT(DISTINCT City) counts only unique cities.

Q5.

Which of the following is NOT a valid aggregate function?

Explanation

LEN() (or LENGTH()) is a single-row string function, not an aggregate function. The others calculate values across multiple rows.

Q6.

Which clause is used to filter groups of results created by GROUP BY?

Explanation

The WHERE clause filters rows before grouping; the HAVING clause filters groups after grouping.

Q7.

Can the WHERE clause be used with aggregate functions?

Explanation

You cannot use aggregate functions in a WHERE clause. You must use HAVING for conditions involving aggregates.

Q8.

Identify the correct sequence of SQL clauses in a SELECT statement.

Explanation

The standard order is SELECT -> FROM -> WHERE -> GROUP BY -> HAVING -> ORDER BY.

Q9.

What will SELECT MAX(Name) FROM Student return?

Explanation

MAX() on a string column returns the highest value in lexicographical (alphabetical) order, which is Z-A.

Q10.

If a table 'Exam' has 5 rows and the 'Marks' column has values: 10, NULL, 20, NULL, 30. What is the output of SELECT COUNT(Marks) FROM Exam?

Explanation

COUNT(column) counts only non-NULL values. There are 3 non-null values (10, 20, 30).

Q11.

Using the same data (10, NULL, 20, NULL, 30), what is the output of SELECT AVG(Marks) FROM Exam?

Explanation

AVG ignores NULLs. The calculation is (10+20+30) / 3 = 60 / 3 = 20.

Q12.

Which statement is true regarding GROUP BY?

Explanation

Standard SQL requires that any column in the SELECT list that is not part of an aggregate function must be included in the GROUP BY clause.

Q13.

What is the output of SELECT ROUND(76.543, 0)?

Explanation

ROUND(x, 0) rounds to the nearest integer. Since .543 is >= .5, it rounds up to 77.

Q14.

Which function returns the remainder of a division?

Explanation

MOD(x, y) returns the remainder of x divided by y.

Q15.

Which function is used to return a specified number of characters from the left side of a string?

Explanation

LEFT(str, len) returns the leftmost 'len' characters from the string 'str'.

Q16.

To sort records by Marks (Descending) and then by Name (Ascending), which query is correct?

Explanation

Primary sort key is Marks (DESC), secondary is Name (ASC).

Q17.

What does SELECT POW(2,3) return?

Explanation

POW(x, y) returns x raised to the power of y. 2^3 = 8.

Q18.

Which function allows you to remove leading and trailing spaces from a string?

Explanation

TRIM() removes both leading and trailing spaces. LTRIM removes leading, RTRIM removes trailing.

Q19.

What will SELECT INSTR('COMPUTER', 'PUT') return?

Explanation

INSTR returns the position of the substring. 'C'(1) 'O'(2) 'M'(3) 'P'(4). 'PUT' starts at index 4.

Q20.

Which SQL function is used to extract a substring starting from a specific position?

Explanation

SUBSTR(str, pos, len) extracts a substring.

Q21.

The query SELECT * FROM Employee ORDER BY Salary is equivalent to:

Explanation

The default sort order is ASC (Ascending), so omitting the keyword implies ASC.

Q22.

What is the result of SELECT UCASE('cbse')?

Explanation

UCASE() or UPPER() converts all characters in the string to uppercase.

Q23.

If we want to count how many distinct Departments exist in a table, we use:

Explanation

To count unique values, the DISTINCT keyword must be placed inside the parenthesis of the COUNT function.

Q24.

Which function returns the current date and time?

Explanation

NOW() returns the current date and time. CURDATE() returns only the date.

Q25.

What is the return type of the DAYNAME() function?

Explanation

DAYNAME() returns the name of the weekday (e.g., 'Monday'), which is a string.

Q26.

Which command displays the current date only?

Explanation

CURDATE() returns the current date (YYYY-MM-DD).

Q27.

What will SELECT TRUNCATE(15.79, 1) return?

Explanation

TRUNCATE simply cuts off the number at the specified decimal without rounding. 15.79 becomes 15.7.

Q28.

A table 'Sales' has columns 'Product' and 'Quantity'. To find the total quantity sold for each product, use:

Explanation

We need to group by 'Product' and calculate the SUM of 'Quantity' for each group.

Q29.

Which function finds the smallest value in a selected column?

Explanation

MIN() is the aggregate function to find the minimum value in a column.

Q30.

What happens if you use HAVING without GROUP BY?

Explanation

In MySQL, if HAVING is used without GROUP BY, it behaves like WHERE, but it is standard practice to use WHERE for rows and HAVING for groups.

Q31.

What is the result of SELECT LENGTH(' India ')?

Explanation

LENGTH() counts all characters including spaces. 2 spaces + 5 letters + 2 spaces = 9.

Q32.

Which wildcard character represents zero, one, or multiple characters in a pattern?

Explanation

The '%' wildcard represents any sequence of zero or more characters.

Q33.

To display names ending with 'a', the condition is:

Explanation

'%a' matches any string ending with the character 'a'.

Q34.

What will SELECT MONTH('2023-10-25') return?

Explanation

MONTH() returns the month number (1-12). October is 10.

Q35.

The aggregate function SUM() works on which data types?

Explanation

SUM() only makes sense for numeric data. You cannot sum strings or dates.

Q36.

Which of the following creates a Cartesian Product?

Explanation

Listing tables in the FROM clause without a WHERE/JOIN condition results in a Cartesian Product (Cross Join).

Q37.

What is the output of SELECT 10 % 3?

Explanation

The % operator (same as MOD) returns the remainder. 10 divided by 3 is 3 with a remainder of 1.

Q38.

To show the average marks of students in each class, but only for classes with average marks > 75:

Explanation

Conditions on aggregate functions (like AVG > 75) must be placed in the HAVING clause.

Q39.

Which function extracts the year part from a date?

Explanation

YEAR(date) returns the 4-digit year from a date value.

Q40.

If you want to sort by a column alias, can you use the alias in the ORDER BY clause?

Explanation

Standard SQL allows using column aliases defined in the SELECT clause within the ORDER BY clause.

Q41.

Which of these is a Single Row Function?

Explanation

ROUND() operates on a single value per row. The others are aggregate functions operating on a set of rows.

Q42.

What is the output of SELECT SIGN(-15)?

Explanation

SIGN(x) returns -1 for negative numbers, 0 for zero, and 1 for positive numbers.

Q43.

Which SQL keyword is used to retrieve only unique values?

Explanation

SELECT DISTINCT column_name ... is used to return unique values.

Q44.

If column 'City' contains 'Delhi', 'Mumbai', 'Delhi', 'Pune', what does COUNT(City) return?

Explanation

COUNT(City) counts all non-null entries, including duplicates. Result is 4.

Q45.

If column 'City' contains 'Delhi', 'Mumbai', 'Delhi', 'Pune', what does COUNT(DISTINCT City) return?

Explanation

COUNT(DISTINCT City) counts unique entries only: Delhi, Mumbai, Pune. Result is 3.

Q46.

What does SELECT RIGHT('Information', 4) return?

Explanation

RIGHT(str, 4) returns the last 4 characters from the right side: 'tion'.

Q47.

Which function is used to join two strings together?

Explanation

CONCAT(str1, str2) concatenates two or more strings.

Q48.

What is the output of SELECT POWER(4, 0.5)?

Explanation

Raising to the power of 0.5 is the same as finding the square root. Square root of 4 is 2.

Q49.

When grouping by multiple columns, the grouping hierarchy is determined by:

Explanation

Data is grouped by the first column listed in GROUP BY, then subdivided by the second, and so on.

Q50.

Which clause is mandatory when using aggregate functions to summary data per category?

Explanation

To summarize data per category (e.g., total sales per city), you must use GROUP BY to define the categories.