Playing as a guest? to earn XP and track your progress.
🧩 Querying Using SQL Quiz
🧩 Querying Using SQL
Which clause is used to sort the result of a query in a specific order?
SORT BY
ORDER BY
ARRANGE BY
GROUP BY
The ORDER BY clause is used to sort the result set in ascending or descending order.
By default, the ORDER BY clause sorts data in which order?
Descending
Ascending
Random
None
If no modifier (ASC/DESC) is specified, ORDER BY defaults to Ascending (ASC) order.
Which aggregate function is used to count the number of rows including NULL values?
COUNT(column_name)
COUNT(*)
SUM(*)
TOTAL()
COUNT(*) counts all rows in the table regardless of NULL values. COUNT(column) ignores NULLs.
What is the purpose of the DISTINCT keyword when used with an aggregate function?
To sort the results
To include duplicate values
To eliminate duplicate values before calculation
To hide the column
DISTINCT eliminates duplicate values. For example, COUNT(DISTINCT City) counts only unique cities.
Which of the following is NOT a valid aggregate function?
SUM()
AVG()
MAX()
LEN()
LEN() (or LENGTH()) is a single-row string function, not an aggregate function. The others calculate values across multiple rows.
Which clause is used to filter groups of results created by GROUP BY?
WHERE
HAVING
ORDER BY
SELECT
The WHERE clause filters rows before grouping; the HAVING clause filters groups after grouping.
Can the WHERE clause be used with aggregate functions?
Yes, always
No, never
Only with COUNT
Only with SUM
You cannot use aggregate functions in a WHERE clause. You must use HAVING for conditions involving aggregates.
Identify the correct sequence of SQL clauses in a SELECT statement.
SELECT, WHERE, FROM, GROUP BY, ORDER BY
SELECT, FROM, GROUP BY, HAVING, ORDER BY
SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY
SELECT, FROM, ORDER BY, WHERE, GROUP BY
The standard order is SELECT -> FROM -> WHERE -> GROUP BY -> HAVING -> ORDER BY.
What will SELECT MAX(Name) FROM Student return?
The name with the most characters
The name that appears last alphabetically
The name that appears first alphabetically
Error
MAX() on a string column returns the highest value in lexicographical (alphabetical) order, which is Z-A.
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?
5
3
2
0
COUNT(column) counts only non-NULL values. There are 3 non-null values (10, 20, 30).
Using the same data (10, NULL, 20, NULL, 30), what is the output of SELECT AVG(Marks) FROM Exam?
12
20
15
Error
AVG ignores NULLs. The calculation is (10+20+30) / 3 = 60 / 3 = 20.
Which statement is true regarding GROUP BY?
It must come before WHERE
It can only group by one column
All non-aggregate columns in SELECT must appear in GROUP BY
It sorts the data automatically in descending order
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.
What is the output of SELECT ROUND(76.543, 0)?
76
77
76.5
76.0
ROUND(x, 0) rounds to the nearest integer. Since .543 is >= .5, it rounds up to 77.
Which function returns the remainder of a division?
DIV()
MOD()
REM()
ROUND()
MOD(x, y) returns the remainder of x divided by y.
Which function is used to return a specified number of characters from the left side of a string?
MID()
RIGHT()
LEFT()
SUBSTR()
LEFT(str, len) returns the leftmost 'len' characters from the string 'str'.
To sort records by Marks (Descending) and then by Name (Ascending), which query is correct?
ORDER BY Marks DESC, Name ASC
ORDER BY Marks ASC, Name DESC
ORDER BY Name ASC, Marks DESC
ORDER BY Marks, Name
Primary sort key is Marks (DESC), secondary is Name (ASC).
What does SELECT POW(2,3) return?
6
5
8
9
POW(x, y) returns x raised to the power of y. 2^3 = 8.
Which function allows you to remove leading and trailing spaces from a string?
LTRIM()
RTRIM()
TRIM()
STRIP()
TRIM() removes both leading and trailing spaces. LTRIM removes leading, RTRIM removes trailing.
What will SELECT INSTR('COMPUTER', 'PUT') return?
0
3
4
5
INSTR returns the position of the substring. 'C'(1) 'O'(2) 'M'(3) 'P'(4). 'PUT' starts at index 4.
Which SQL function is used to extract a substring starting from a specific position?
EXTRACT()
SUBSTR()
GET()
PULL()
SUBSTR(str, pos, len) extracts a substring.
The query SELECT * FROM Employee ORDER BY Salary is equivalent to:
ORDER BY Salary DESC
ORDER BY Salary ASC
ORDER BY Salary RANDOM
GROUP BY Salary
The default sort order is ASC (Ascending), so omitting the keyword implies ASC.
What is the result of SELECT UCASE('cbse')?
Cbse
cbse
CBSE
cBSE
UCASE() or UPPER() converts all characters in the string to uppercase.
If we want to count how many distinct Departments exist in a table, we use:
SELECT COUNT(Department) ...
SELECT COUNT(*) ...
SELECT DISTINCT COUNT(Department) ...
SELECT COUNT(DISTINCT Department) ...
To count unique values, the DISTINCT keyword must be placed inside the parenthesis of the COUNT function.
Which function returns the current date and time?
DATE()
NOW()
TODAY()
CURDATE()
NOW() returns the current date and time. CURDATE() returns only the date.
What is the return type of the DAYNAME() function?
Number
String
Date
Boolean
DAYNAME() returns the name of the weekday (e.g., 'Monday'), which is a string.
Which command displays the current date only?
SELECT NOW();
SELECT CURTIME();
SELECT CURDATE();
SELECT DATE();
CURDATE() returns the current date (YYYY-MM-DD).
What will SELECT TRUNCATE(15.79, 1) return?
15.8
15.7
15
16
TRUNCATE simply cuts off the number at the specified decimal without rounding. 15.79 becomes 15.7.
A table 'Sales' has columns 'Product' and 'Quantity'. To find the total quantity sold for each product, use:
SELECT Product, SUM(Quantity) FROM Sales GROUP BY Product
SELECT Product, COUNT(Quantity) FROM Sales GROUP BY Product
SELECT Product, SUM(Quantity) FROM Sales ORDER BY Product
SELECT SUM(Quantity) FROM Sales
We need to group by 'Product' and calculate the SUM of 'Quantity' for each group.
Which function finds the smallest value in a selected column?
LEAST()
MIN()
SMALL()
LOW()
MIN() is the aggregate function to find the minimum value in a column.
What happens if you use HAVING without GROUP BY?
It acts like a WHERE clause
It causes a syntax error
It sorts the data
It deletes the data
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.
What is the result of SELECT LENGTH(' India ')?
5
7
9
Error
LENGTH() counts all characters including spaces. 2 spaces + 5 letters + 2 spaces = 9.
Which wildcard character represents zero, one, or multiple characters in a pattern?
_
?
%
#
The '%' wildcard represents any sequence of zero or more characters.
To display names ending with 'a', the condition is:
WHERE Name LIKE 'a%'
WHERE Name LIKE '%a'
WHERE Name = '%a'
WHERE Name LIKE 'a_'
'%a' matches any string ending with the character 'a'.
What will SELECT MONTH('2023-10-25') return?
October
10
25
2023
MONTH() returns the month number (1-12). October is 10.
The aggregate function SUM() works on which data types?
Numeric only
String only
Date only
All data types
SUM() only makes sense for numeric data. You cannot sum strings or dates.
Which of the following creates a Cartesian Product?
SELECT * FROM T1, T2
SELECT * FROM T1 JOIN T2 ON T1.id = T2.id
SELECT * FROM T1 WHERE id = 5
SELECT * FROM T1 GROUP BY id
Listing tables in the FROM clause without a WHERE/JOIN condition results in a Cartesian Product (Cross Join).
What is the output of SELECT 10 % 3?
3.33
3
1
10
The % operator (same as MOD) returns the remainder. 10 divided by 3 is 3 with a remainder of 1.
To show the average marks of students in each class, but only for classes with average marks > 75:
WHERE AVG(Marks) > 75
HAVING AVG(Marks) > 75
ORDER BY AVG(Marks) > 75
GROUP BY AVG(Marks) > 75
Conditions on aggregate functions (like AVG > 75) must be placed in the HAVING clause.
Which function extracts the year part from a date?
YEAR()
GETYEAR()
EXTRACTYEAR()
YYYY()
YEAR(date) returns the 4-digit year from a date value.
If you want to sort by a column alias, can you use the alias in the ORDER BY clause?
Yes
No
Only in GROUP BY
Only in WHERE
Standard SQL allows using column aliases defined in the SELECT clause within the ORDER BY clause.
Which of these is a Single Row Function?
MAX()
MIN()
AVG()
ROUND()
ROUND() operates on a single value per row. The others are aggregate functions operating on a set of rows.
What is the output of SELECT SIGN(-15)?
-1
1
0
15
SIGN(x) returns -1 for negative numbers, 0 for zero, and 1 for positive numbers.
Which SQL keyword is used to retrieve only unique values?
UNIQUE
DISTINCT
DIFFERENT
SINGLE
SELECT DISTINCT column_name ... is used to return unique values.
If column 'City' contains 'Delhi', 'Mumbai', 'Delhi', 'Pune', what does COUNT(City) return?
3
4
2
Error
COUNT(City) counts all non-null entries, including duplicates. Result is 4.
If column 'City' contains 'Delhi', 'Mumbai', 'Delhi', 'Pune', what does COUNT(DISTINCT City) return?
3
4
2
Error
COUNT(DISTINCT City) counts unique entries only: Delhi, Mumbai, Pune. Result is 3.
What does SELECT RIGHT('Information', 4) return?
Info
tion
rmat
ation
RIGHT(str, 4) returns the last 4 characters from the right side: 'tion'.
Which function is used to join two strings together?
JOIN()
MERGE()
CONCAT()
ADD()
CONCAT(str1, str2) concatenates two or more strings.
What is the output of SELECT POWER(4, 0.5)?
2
16
8
4.5
Raising to the power of 0.5 is the same as finding the square root. Square root of 4 is 2.
When grouping by multiple columns, the grouping hierarchy is determined by:
The order of columns in the SELECT clause
The order of columns in the GROUP BY clause
Alphabetical order of column names
Random order
Data is grouped by the first column listed in GROUP BY, then subdivided by the second, and so on.
Which clause is mandatory when using aggregate functions to summary data per category?
ORDER BY
WHERE
GROUP BY
HAVING
To summarize data per category (e.g., total sales per city), you must use GROUP BY to define the categories.