Exit Chapter 10 ยท ๐Ÿ” Simple Queries in SQL โ€” Quiz

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

Question 1 of 60 0 correct so far

๐Ÿ” Simple Queries in SQL Quiz

Q1.

Which of the following is a correct String Literal in MySQL?

Explanation

String literals must be enclosed in single quotes. 'Computer Science' is a string literal. 25 is a numeric literal, TRUE is a boolean literal, and 2026-06-27 without quotes is not valid as a date literal.

Q2.

Which data type stores an exact decimal number and is recommended for storing salary or price fields?

Explanation

DECIMAL(m,d) stores exact decimal numbers. FLOAT is approximate and can cause rounding errors. For financial data like salary or price, always use DECIMAL.

Q3.

A column is defined as CHAR(10). A value 'Hi' is stored in it. How many characters of storage does it actually use?

Explanation

CHAR is fixed-length โ€” it always reserves exactly n characters of storage regardless of the actual data length, padding with spaces if needed. CHAR(10) always uses 10 characters.

Q4.

What does a NULL value represent in MySQL?

Explanation

NULL represents missing, unknown, or inapplicable data. It is NOT the same as 0 (zero) or '' (empty string), which are actual known values.

Q5.

Which of the following is a valid single-line comment in MySQL?

Explanation

MySQL supports -- (double dash) and # (hash) for single-line comments, and /* */ for multi-line comments. // is used in languages like Java/C++ but not SQL.

Q6.

Are SQL keywords like SELECT and WHERE case-sensitive?

Explanation

SQL keywords (SELECT, WHERE, FROM, etc.) are NOT case-sensitive. SELECT, select, and Select all work identically. However, data values in string columns may be case-sensitive depending on the database collation.

Q7.

What symbol is used at the end of every SQL statement to execute it?

Explanation

Every SQL statement must end with a semicolon (;). This tells MySQL that the statement is complete and ready to be executed.

Q8.

Which SQL command lists all the databases available on the MySQL server?

Explanation

SHOW DATABASES; lists all databases available on the MySQL server. SHOW TABLES; lists all tables inside the currently selected database.

Q9.

What does SELECT * FROM Employee; return?

Explanation

The asterisk (*) is a wildcard meaning 'all columns'. SELECT * FROM Employee; retrieves every column and every row from the Employee table.

Q10.

Based on the Employee table, what is the output of: SELECT DISTINCT Dept FROM Employee;

Explanation

DISTINCT removes duplicates. The Employee table has Sales (twice), IT (twice), HR (twice). After DISTINCT, only one of each appears: Sales, IT, HR.

Q11.

What is the purpose of the DISTINCT keyword in a SELECT statement?

Explanation

DISTINCT eliminates duplicate rows from the query result, ensuring each unique value or combination of values appears only once in the output.

Q12.

What does SELECT ALL Dept FROM Employee; return compared to SELECT Dept FROM Employee;?

Explanation

ALL is the default behaviour of SELECT โ€” it returns all rows including duplicates. SELECT ALL Dept and SELECT Dept produce identical output. ALL is rarely used explicitly.

Q13.

Which command displays the structure of the Employee table (column names, data types, constraints) without showing any data?

Explanation

DESCRIBE table_name; (or DESC table_name;) shows the table's structure including field names, data types, whether NULL is allowed, and key constraints โ€” without displaying the actual data rows.

Q14.

What is the result of SELECT 17 % 5; in MySQL?

Explanation

% is the modulus operator, which returns the remainder after division. 17 divided by 5 is 3 with a remainder of 2. So 17 % 5 = 2.

Q15.

Which query correctly calculates annual salary (monthly salary multiplied by 12) for all employees?

Explanation

Salary * 12 is a scalar expression that multiplies the Salary column value by 12 for every row, producing the annual salary.

Q16.

What does a column alias do in a SELECT statement?

Explanation

A column alias (using AS) gives a column a temporary display name in the query output only. It does NOT change the actual column name in the table.

Q17.

Which query gives the output column the heading 'AnnualSalary' instead of 'Salary * 12'?

Explanation

The AS keyword followed by the alias name creates a column alias. SELECT Name, Salary * 12 AS AnnualSalary renames the output column to 'AnnualSalary'.

Q18.

Anjali Gupta has a NULL Salary. What will SELECT Salary * 12 FROM Employee WHERE Name = 'Anjali Gupta'; return?

Explanation

Any arithmetic operation involving NULL returns NULL. NULL * 12 = NULL because 'unknown times anything' is still unknown.

Q19.

Which function replaces a NULL value with a specified default value?

Explanation

IFNULL(col, default) returns the column value if it is not NULL, or returns the specified default value if the column is NULL. e.g., IFNULL(Salary, 0) returns 0 when Salary is NULL.

Q20.

What is the purpose of the WHERE clause in a SELECT statement?

Explanation

The WHERE clause filters records, returning only the rows that satisfy the specified Boolean condition. It controls WHICH rows are included in the result.

Q21.

Based on the Employee table, how many rows does this query return? SELECT Name FROM Employee WHERE Salary > 50000;

Explanation

Employees with Salary > 50000 are Arjun Kumar (62000) and Rohan Das (71000). That is 2 rows. Anjali Gupta's NULL salary does not satisfy the condition.

Q22.

Which operator checks if a value is NOT equal to another value in SQL?

Explanation

Both != and <> mean 'not equal to' in MySQL. Both are valid and produce the same result.

Q23.

Which query returns employees who work in IT AND earn more than 60000?

Explanation

AND requires BOTH conditions to be true simultaneously. OR would return employees who work in IT OR who earn more than 60000 (not necessarily both).

Q24.

Based on the Employee table, what is the output of: SELECT Name FROM Employee WHERE Dept = 'HR' OR Dept = 'Sales';

Explanation

OR returns rows where AT LEAST ONE condition is true. HR employees are Sita Roy and Vikram Singh. Sales employees are Priya Sharma and Anjali Gupta. Together that is 4 employees.

Q25.

The BETWEEN operator in SQL is:

Explanation

BETWEEN is inclusive of BOTH endpoints. WHERE Salary BETWEEN 40000 AND 65000 is equivalent to WHERE Salary >= 40000 AND Salary <= 65000.

Q26.

Based on the Employee table, how many rows does this query return? SELECT Name FROM Employee WHERE Salary BETWEEN 40000 AND 65000;

Explanation

Employees with Salary between 40000 and 65000 (inclusive): Priya Sharma (45000), Arjun Kumar (62000), Vikram Singh (41000). That is 3 rows. Anjali's NULL is excluded.

Q27.

Which query is equivalent to: WHERE Dept = 'HR' OR Dept = 'Sales'?

Explanation

The IN operator checks if a value matches any value in a list. WHERE Dept IN ('HR', 'Sales') is exactly equivalent to WHERE Dept = 'HR' OR Dept = 'Sales'.

Q28.

What does the % wildcard represent in a LIKE pattern?

Explanation

% matches any sequence of zero or more characters. For example, 'A%' matches any string starting with A, regardless of what follows.

Q29.

What does the _ wildcard represent in a LIKE pattern?

Explanation

_ (underscore) matches exactly ONE character. For example, '_ohan' matches 'Rohan' because _ matches 'R' and 'ohan' matches the rest.

Q30.

Based on the Employee table, what does this query return? SELECT Name FROM Employee WHERE Name LIKE 'A%';

Explanation

'A%' matches any name starting with 'A'. Both Arjun Kumar and Anjali Gupta start with 'A', so both are returned.

Q31.

Which query finds all employees whose name ends with the letter 'a'?

Explanation

'%a' means any sequence of characters followed by 'a' at the end. The % at the beginning matches everything before the last character 'a'.

Q32.

Which query correctly finds employees whose Salary is NULL?

Explanation

NULL cannot be tested with = because NULL represents 'unknown'. You must use IS NULL. WHERE Salary = NULL always returns zero rows even when NULLs exist.

Q33.

Based on the Employee table, how many rows does SELECT Name FROM Employee WHERE Salary IS NOT NULL; return?

Explanation

Only Anjali Gupta (EmpId 105) has a NULL Salary. All other 5 employees have non-NULL salaries, so IS NOT NULL returns 5 rows.

Q34.

In operator precedence, which logical operator is evaluated BEFORE the others?

Explanation

The order is: Parentheses > Arithmetic > Comparison > NOT > AND > OR. AND is evaluated before OR, which means conditions joined by AND are grouped first.

Q35.

Consider: WHERE Dept = 'IT' OR Dept = 'HR' AND Salary > 40000. Due to operator precedence, this is interpreted as:

Explanation

AND has higher precedence than OR, so it binds tighter. The condition is interpreted as: IT employees (any salary) OR (HR employees with Salary > 40000).

Q36.

What does ORDER BY Salary DESC do?

Explanation

DESC means descending order โ€” from the largest value to the smallest. ASC (ascending) is the default and goes from smallest to largest.

Q37.

What is the default sort order when ORDER BY is used without ASC or DESC?

Explanation

ASC (ascending) is the default sort order in SQL. If you write ORDER BY Salary, it is the same as ORDER BY Salary ASC โ€” smallest values appear first.

Q38.

What does UPPER('priya') return in MySQL?

Explanation

UPPER() converts all characters in a string to uppercase. UPPER('priya') returns 'PRIYA'.

Q39.

What does LENGTH('Priya') return?

Explanation

LENGTH() returns the number of characters in a string. 'Priya' has 5 characters: P, r, i, y, a.

Q40.

What does CONCAT('Hello, ', Name) do for the row where Name = 'Priya Sharma'?

Explanation

CONCAT() joins (concatenates) multiple strings together in the order given. CONCAT('Hello, ', 'Priya Sharma') produces 'Hello, Priya Sharma'.

Q41.

What does SUBSTRING('Computer', 1, 4) return?

Explanation

SUBSTRING(str, pos, len) extracts a substring starting at position pos (1-indexed) with length len. SUBSTRING('Computer', 1, 4) extracts 4 characters starting at position 1: 'Comp'.

Q42.

What does ROUND(45.678, 2) return?

Explanation

ROUND(num, d) rounds to d decimal places. The third decimal is 8 (โ‰ฅ5), so the second decimal is rounded up: 45.678 rounded to 2 places = 45.68.

Q43.

What does TRUNCATE(45.678, 1) return?

Explanation

TRUNCATE(num, d) cuts off digits after d decimal places WITHOUT rounding. TRUNCATE(45.678, 1) simply drops everything after the first decimal: 45.6.

Q44.

Which function returns the current date only (without time) in MySQL?

Explanation

CURDATE() returns the current date only (e.g., '2026-06-27'). NOW() returns current date AND time. CURTIME() returns current time only.

Q45.

Which query displays the joining year for each employee from the Employee table?

Explanation

YEAR(date) extracts and returns the year portion from a DATE value. YEAR(DOJ) for Priya Sharma (DOJ: 2022-03-15) returns 2022.

Q46.

What do aggregate functions like SUM, AVG, MAX, and MIN do with NULL values?

Explanation

Aggregate functions (SUM, AVG, MAX, MIN, COUNT with column name) silently ignore NULL values. Anjali Gupta's NULL salary is not included in SUM, AVG, MAX, or MIN calculations.

Q47.

Based on the Employee table, what does SELECT COUNT(*) FROM Employee; return?

Explanation

COUNT(*) counts ALL rows regardless of NULL values. The Employee table has 6 rows, so COUNT(*) returns 6.

Q48.

Based on the Employee table, what does SELECT COUNT(Salary) FROM Employee; return?

Explanation

COUNT(column_name) counts only non-NULL values in that column. Anjali Gupta's Salary is NULL, so it is excluded. 6 rows - 1 NULL = 5.

Q49.

Based on the Employee table, what does SELECT SUM(Salary) FROM Employee; return? (Salaries: 45000, 62000, 38000, 71000, NULL, 41000)

Explanation

SUM adds all non-NULL values: 45000 + 62000 + 38000 + 71000 + 41000 = 257000. The NULL value for Anjali Gupta is ignored.

Q50.

Based on the Employee table, what does SELECT AVG(Salary) FROM Employee; return? (Non-NULL salaries: 45000, 62000, 38000, 71000, 41000)

Explanation

AVG ignores NULL values. Sum of 5 non-NULL salaries = 257000. AVG = 257000 / 5 = 51400.00. Note: AVG divides by the count of non-NULL values (5), not total rows (6).

Q51.

Which query finds the name of the highest-paid employee?

Explanation

MAX(Salary) alone returns 71000, not the name. To find the name of the employee with MAX salary, you use a subquery or WHERE with the MAX value. Option C uses a subquery correctly.

Q52.

What is the correct SQL to display employee names in all uppercase letters?

Explanation

UPPER(str) is the MySQL string function that converts a string to all uppercase characters. UPPER(Name) converts the Name value for each row to uppercase.

Q53.

What does SELECT Name FROM Employee WHERE Name LIKE '_i%'; return from the Employee table?

Explanation

'_i%' means: any one character, then 'i', then anything. Sita Roy: S(1 char) + i + ta Roy matches. Vikram Singh: V(1 char) + i + kram Singh matches. Priya has 'P' then 'r', not 'i'.

Q54.

Which query displays employees from Delhi sorted by Name in ascending order?

Explanation

WHERE must come before ORDER BY in SQL syntax. ASC is the correct keyword for ascending alphabetical order. SORT BY is not valid SQL.

Q55.

What does SELECT POWER(2, 3) return?

Explanation

POWER(m, n) raises m to the power n. POWER(2, 3) = 2ยณ = 2 ร— 2 ร— 2 = 8.

Q56.

Which of the following correctly displays each employee's name along with the text 'works in' and their department as a single combined column?

Explanation

CONCAT() joins multiple strings into one column. The + operator does not work for string concatenation in MySQL (it does arithmetic). CONCAT(Name, ' works in ', Dept) produces a single readable column.

Q57.

Based on the Employee table, what does SELECT MIN(Salary) FROM Employee; return?

Explanation

MIN() returns the smallest non-NULL value. The salaries (excluding NULL) are: 45000, 62000, 38000, 71000, 41000. The minimum is 38000 (Sita Roy).

Q58.

Which SQL statement has a syntax error?

Explanation

WHERE Salary = NULL is not a syntax error (MySQL accepts it) but it is a logical error โ€” it will always return zero rows because NULL cannot be compared using =. IS NULL must be used to find NULL values.

Q59.

What is the output of SELECT ABS(-15); in MySQL?

Explanation

ABS() returns the absolute (positive) value of a number. ABS(-15) = 15.

Q60.

A student writes SELECT Name FROM Employee WHERE City = 'delhi';. The Employee table stores cities as 'Delhi' (capital D). What will MySQL return by default?

Explanation

MySQL's default collation (utf8_general_ci, where 'ci' means case-insensitive) makes string comparisons case-insensitive. So 'delhi' matches 'Delhi' by default. This behaviour can change with different collation settings.