Q.Differentiate between COUNT ( ) and COURT (*) functions in MYSQL. Give suitable examples to support your answer.
🔒You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
🔒 Start your 14-day free trial to unlock the full solution →Concept understanding — SQL Aggregate Functions
Imagine you have a giant box of receipts from a month of sales at a small shop. You don't want to look at every single receipt one by one — you want a single, useful number that summarises the whole pile. How much total money came in? What was the average sale? How many customers bought something? That instinct — to collapse a pile of raw details into one meaningful number — is exactly what an SQL aggregate function does.
In a database, a table might hold thousands of rows of data: one row for every sale, every student, every product. An aggregate function takes all those rows in a column and boils them down to a single value. It doesn't change the data; it answers a question about the whole set. The most common ones are COUNT, SUM, AVG, MIN, and MAX. Each one has a clear, everyday meaning.
COUNT tells you how many rows there are — like counting how many students are in a class list. SUM adds up all the numbers in a column — like totalling the marks of every student in an exam. AVG gives the average of those numbers. MIN and MAX find the smallest and largest values — the lowest temperature recorded or the highest sale of the day.
Aggregate functions work on a column of data, not across rows. You always write them as FUNCTION_NAME(column_name). For example, AVG(price) gives the average of all prices in that column.
The real power comes when you combine them with the GROUP BY clause. Without GROUP BY, an aggregate function summarises the entire table. With GROUP BY, you split the table into groups first, then apply the function to each group separately. Think of it like this: you don't just want the average sale for the whole year — you want the average sale per month. GROUP BY month creates twelve groups, and AVG(sale_amount) runs once for each group, giving you twelve averages.
This is why aggregate functions matter in business, economics, and any data-driven decision. A manager doesn't ask "what was every single transaction?" They ask "what was the total revenue last quarter?" or "which product category had the highest average rating?" Aggregate functions turn raw, overwhelming data into actionable summaries. …
Concept: Aggregate functions for counting rows in SQL queries.
There appears to be a typo in the question — MySQL has COUNT() but no COURT() function. The intended comparison is likely COUNT(column_name) versus COUNT(*).
Both count rows, but they handle NULL values differently:
COUNT(*)counts all rows in the result set, including those withNULLvalues in any column.COUNT(column_name)counts only rows where the specified column is notNULL.
Example: Consider a table Students:
| ID | Name | |
|---|---|---|
| 1 | Raj | raj@example.com |
| 2 | Priya | NULL |
| 3 | Amit | amit@example.com |
COUNT() is an aggregate function that counts rows or non-NULL values in SQL; COURT(*) does not exist in MySQL — the question likely confuses COUNT(*) (count all rows) with COUNT(column) (count non-NULL values in a column).
There appears to be a typo in the question. MySQL has no function called COURT(*). The standard aggregate function is COUNT(), which comes in two main forms: COUNT(*) and COUNT(column_name). Understanding the difference between these two is essential for writing correct queries.
The Concept: What COUNT() Does
COUNT() is an aggregate function that returns the number of rows that match a criterion. The key distinction lies in what you ask it to count:
COUNT(*)counts every row in the result set, regardless of NULL values.COUNT(column_name)counts only rows where that specific column is NOT NULL.
This difference becomes critical when your table contains NULL values, which represent missing or unknown data.
The Two Forms of COUNT()
1. COUNT(*) — Count All Rows
COUNT(*) counts every row returned by the query, including rows where some or all columns are NULL.
Example:
Suppose we have a table students:
| id | name | |
|---|---|---|
| 1 | Arjun | arjun@example.com |
| 2 | Priya | NULL |
| 3 | Rohan | rohan@example.com |
| 4 | Sneha | NULL |
SELECT COUNT(*) FROM students;
Result: 4
All four rows are counted, even though two have NULL emails.
2. COUNT(column_name) — Count Non-NULL Values
COUNT(column_name) counts only the rows where the specified column contains a non-NULL value.
Example (same table):
SELECT COUNT(email) FROM students;
Result: 2
Only Arjun and Rohan have email addresses; Priya and Sneha's NULL emails are excluded.
Side-by-Side Comparison
| Function | What It Counts | Treats NULL as |
|---|---|---|
COUNT(*) | All rows in the result set | Counted |
COUNT(column_name) | Rows where column_name IS NOT NULL | Ignored |
Practical Use Cases
When to use COUNT(*): …
- CBSE 2026Set 91/41 markMCQQ.Which aggregate function in SQL returns the smallest value from a column in a table ? (A) MIN() (B) MAX() (C) SMALL() (D) LOWER()
›Reveal solutionSolution
The aggregate function that returns the smallest value from a column in SQL is MIN().
In SQL, aggregate functions are used to perform calculations on a set of rows and return a single summary value. They are a core part of querying databases, especially when you need to analyse data — finding totals, averages, counts, or extremes. Among these, the function that specifically returns the smallest value from a column is
MIN().The
MIN()function works on numeric, date, or text columns. For text, it returns the value that comes first alphabetically. For dates, it returns the earliest date. This makes it versatile for many real-world queries — for example, finding the lowest price in a products table, the earliest order date, or the shortest employee name.NoteThe
MIN()function ignores NULL values by default. If a column has NULLs, they are simply not considered in the result.Let’s look at the options given:
- (A) MIN() — Correct. This is the standard SQL aggregate function for the smallest value.
- (B) MAX() — Returns the largest value, not the smallest.
- (C) SMALL() — This is not a valid SQL aggregate function. No such function exists in standard SQL or in any major database system. …
- CBSE 2026Set 90/1/11 markQ.State whether the following statement is True or False : In SQL, an aggregate function returns multiple values for each column on which it is applied.
›Reveal solutionSolution
Aggregate functions in SQL collapse multiple rows into a single summary value per group. The statement is False.
Why aggregate functions exist
SQL aggregate functions exist to summarize data across rows. When you have a table with thousands of transactions and want to know the total sales, you don't want thousands of numbers back—you want one number that represents the sum. That's the entire purpose of aggregation: to reduce many values to a single, meaningful summary.
Think of it this way: if you ask "What is the average age of students in this class?", you expect one number as the answer, not a list repeating some value for each student. Aggregate functions work the same way.
How aggregate functions actually behave
The standard aggregate functions in SQL are:
Function Purpose Output COUNT()Counts rows Single number SUM()Adds values Single number AVG()Computes mean Single number MAX()Finds maximum Single value MIN()Finds minimum Single value Each of these takes multiple input rows and produces one output value.
For example:
SELECT AVG(salary) FROM employees;If the
employeestable has 500 rows, this query returns exactly one row with one value—the average salary across all 500 employees.What about GROUP BY?
When you use
GROUP BY, aggregate functions return one value per group, not one value per original row:SELECT department, AVG(salary) FROM employees GROUP BY department; ``` … - CBSE 2026Set 90/1/11 markMCQQ.Q. 20 and Q. 21 are Assertion (A) and Reason (R) Type questions. Choose the correct option as : (A) Both (A) and (R) are True, and (R) correctly explains (A). (B) Both (A) and (R) are True, but (R) does not correctly explain (A). (C) (A) is True, but (R) is False. (D) (A) is False, but (R) is True. Assertion (A) : The output of the SQL query SELECT COUNT (name) FROM students; will differ from the output of SELECT COUNT () FROM students; if there are some NULL values in the name column. Reason (R) : COUNT(column_name) returns the count of NON NULL values in that column, whereas COUNT() returns the number of records in the table.
›Reveal solutionSolution
COUNT(column_name)excludes NULL values whileCOUNT(*)counts all rows; therefore the two queries will differ when the name column contains NULLs. Both assertion and reason are true, and the reason correctly explains the assertion.The heart of this question lies in understanding how SQL's
COUNTfunction behaves differently depending on what you pass to it.When you write
COUNT(*), you're asking SQL to count every row in the table, regardless of whether any particular column contains NULL values. It's a row counter, pure and simple.When you write
COUNT(column_name), you're asking SQL to count only those rows wherecolumn_nameis NOT NULL. SQL silently skips over any row where that column holds a NULL value.This distinction matters enormously in practice. Imagine a
studentstable with 100 rows, but 5 students have NULL in thenamecolumn (perhaps their names weren't recorded yet). Then:SELECT COUNT(*) FROM students;returns 100 (all rows)SELECT COUNT(name) FROM students;returns 95 (only non-NULL names)
Now let's evaluate the assertion and reason:
Assertion (A): "The output of
SELECT COUNT(name) FROM students;will differ fromSELECT COUNT(*) FROM students;if there are some NULL values in the name column."This is true. As we just saw, the presence of NULLs in the
namecolumn causesCOUNT(name)to return a smaller number thanCOUNT(*). … - CBSE 2025Set 91/41 markMCQQ.Which aggregate function in SQL displays the number of values in the specified column ignoring the NULL values ? (A) len() (B) count() (C) number() (D) num()
›Reveal solutionSolution
The
COUNT()aggregate function in SQL is used to display the number of values in a specified column, inherently ignoring NULL values.In SQL, aggregate functions are powerful tools designed to perform calculations on a set of rows and return a single summary value. These functions are fundamental for data analysis, allowing us to derive insights like sums, averages, maximums, minimums, and counts from large datasets. When dealing with real-world data, it's common to encounter
NULLvalues, which represent missing or unknown data. How theseNULLvalues are handled by aggregate functions is a crucial aspect of understanding their behavior.The question specifically asks for an aggregate function that counts the number of values in a column while ignoring
NULLentries. Among the standard SQL aggregate functions,COUNT()is precisely designed for this purpose.Let's break down how
COUNT()works:-
COUNT(column_name): When you specify a column name inside theCOUNT()function, likeCOUNT(EmployeeID)orCOUNT(ProductName), SQL will iterate through each row in that column. For every row where the specifiedcolumn_namehas a non-NULL value, it increments the count. Rows where thecolumn_nameisNULLare simply skipped and do not contribute to the total count. This is the exact behavior requested by the question. -
COUNT(*): It's important to distinguishCOUNT(column_name)fromCOUNT(*). TheCOUNT(*)function counts the total number of rows in the result set, regardless of whether any specific column in those rows containsNULLvalues. It essentially counts the rows themselves, not the non-NULL values within a particular column.
ImportantCOUNT(column_name)counts only non-NULL values in the specified column, whereasCOUNT(*)counts all rows, irrespective of NULLs in any column.Now, let's consider the other options provided: …
-
- CBSE 2025Set 90/1/11 markMCQQ.Which of the following is not an aggregate function in SQL? (A) COUNT(*) (B) MIN() (C) LEFT() (D) AVG()
›Reveal solutionSolution
LEFT() is a string manipulation function, not an aggregate function — it extracts characters from the left side of a string, while COUNT(*), MIN(), and AVG() all perform calculations across multiple rows.
Aggregate functions in SQL are designed to perform calculations on a set of values and return a single summary value. They work across multiple rows of data, collapsing them into one meaningful result. Think of them as tools that answer questions like "How many records are there?", "What's the smallest value?", or "What's the average?" — they digest entire columns of data and give you one number back.
COUNT(*) counts the total number of rows in a result set, including those with NULL values. It's the go-to function when you need to know "how many" of something exists in your database.
MIN() finds the smallest value in a column. Whether you're looking for the lowest price, the earliest date, or the minimum score, MIN() scans through all the rows and returns that single smallest value.
AVG() calculates the arithmetic mean of a numeric column. It adds up all the values and divides by the count, giving you the average — essential for understanding central tendencies in your data.
Now LEFT() operates on an entirely different principle. It's a string function that extracts a specified number of characters from the left side of a text string. For example, LEFT('Database', 4) would return 'Data'. Notice what's happening here: LEFT() works on a single string value at a time, not across multiple rows. It doesn't summarize or aggregate anything — it simply manipulates one piece of text. …
- CBSE 2024Set 91/41 markMCQQ.In SQL, the aggregate function which will display the cardinality of the table is ____. (A) sum() (B) count() (C) avg() (D) sum()
›Reveal solutionSolution
The
count(*)function returns the total number of rows in a table, which is its cardinality.When we talk about the cardinality of a table in database terminology, we mean the number of rows (or tuples) it contains. It's a fundamental measure of the table's size — how many records exist in that relation. SQL provides a family of aggregate functions that perform calculations across multiple rows and return a single summary value, and understanding which one gives us cardinality is essential for querying databases effectively.
Aggregate functions operate on sets of values from a column (or columns) and collapse them into one result. The most common ones include:
sum()— adds up all the values in a numeric columnavg()— calculates the arithmetic mean of values in a numeric columncount()— counts rows or non-null valuesmax()andmin()— find the largest or smallest value
The question asks specifically which function reveals how many rows are in the table. The
count(*)function does exactly this: it counts every row in the table, regardless of whether individual columns contain null values or not. The asterisk*tells SQL to count all rows without filtering by any particular column's content. … - CBSE 2023Set 90/1/11 markMCQQ.Which of the following is not a valid aggregate function in MYSQL? (A) COUNT ( ) (B) SUM ( ) (C) MAX ( ) (D) LEN ( )
›Reveal solutionSolution
LEN()is not a valid aggregate function in MySQL; it is a scalar string function that returns the length of a single string.In the world of databases, particularly when working with SQL (Structured Query Language) like MySQL, we often need to perform calculations that summarize data across multiple records. This is where aggregate functions come into play. These functions operate on a collection of rows and return a single value that represents a summary of that collection. Think of them as tools to get insights like "total sales," "average score," or "number of students."
Let's break down what aggregate functions are and then examine the options provided.
Understanding Aggregate Functions
An aggregate function processes a group of input values (from multiple rows) and produces a single output value. They are typically used with the
SELECTstatement and often combined with theGROUP BYclause to perform calculations on specific groups of data. WithoutGROUP BY, the aggregate function treats the entire result set as a single group.Common aggregate functions include:
COUNT(): This function counts the number of rows or non-NULL values in a specified column. For example,COUNT(*)counts all rows, whileCOUNT(column_name)counts rows wherecolumn_nameis not NULL.SUM(): This function calculates the total sum of a numeric column. It's useful for finding totals like the sum of quantities or prices.AVG(): This function computes the average (arithmetic mean) of a numeric column. It's perfect for finding average scores, average salaries, etc.MAX(): This function finds the maximum value in a specified column. It can be used with numeric, string, or date/time data types to find the highest value.MIN(): This function finds the minimum value in a specified column. Similar toMAX(), it works with numeric, string, or date/time data types to find the lowest value.
ImportantThe key characteristic of an aggregate function is that it takes multiple input values (from a set of rows) and returns a single, summarized output value.
Analyzing the Options
Now, let's look at the functions given in the question:
-
(A)
COUNT(): As discussed,COUNT()is a fundamental aggregate function. It is used to count the number of rows that match a specified criterion. For instance,SELECT COUNT(student_id) FROM Students;would tell you the total number of students. This is a valid aggregate function in MySQL. -
(B)
SUM():SUM()is also a core aggregate function. It calculates the total sum of values in a numeric column. For example,SELECT SUM(amount) FROM Orders;would give you the total amount of all orders. This is a valid aggregate function in MySQL. …
- CBSE 2020Set 91/D1 markQ.Which SQL aggregate function is used to count all records of a table?
›Reveal solutionSolution
The SQL aggregate function used to count all records in a table is
COUNT(*).When you work with databases, one of the most common tasks is simply knowing how many rows exist in a table. SQL provides a set of functions called aggregate functions that perform a calculation over a set of values and return a single result. Among these, the one designed specifically for counting records is
COUNT().COUNT()comes in two main forms.COUNT(*)counts every row in the table, regardless of whether any column contains a NULL value — the most straightforward way to get the total number of records.COUNT(column_name)counts only those rows where the specified column has a non-NULL value. So for the absolute total number of rows,COUNT(*)is the correct choice.NoteIn SQL, a NULL value represents missing or unknown data.
COUNT(*)includes rows with NULLs in any column, whileCOUNT(column_name)skips rows where that specific column is NULL. … - CBSE 2019Set 90/1/11 markQ.Consider the following table ‘Transporter’ that stores the order details about items to be transported. Table : TRANSPORTERWrite SQL command for the following statement :
ORDERNO DRIVERNAME DRIVERGRADE ITEM TRAVELDATE DESTINATION 10012 RAM YADAV A TELEVISION 2019-04-19 MUMBAI 10014 SOMNATH SINGH FURNITURE 2019-01-12 PUNE 10016 MOHAN VERMA B WASHING MACHINE 2019-06-06 LUCKNOW 10018 RISHI SINGH A REFRIGERATOR 2019-04-07 MUMBAI 10019 RADHE MOHAN TELEVISION 2019-05-30 UDAIPUR 10020 BISHEN PRATAP B REFRIGERATOR 2019-05-02 MUMBAI 10021 RAM TELEVISION 2019-05-03 PUNE (vi) To display the number of drivers who have ‘MOHAN’ anywhere in their names.›Reveal solutionSolution
Use
COUNT(*)with theLIKE '%MOHAN%'pattern: the%wildcard on both sides matches 'MOHAN' anywhere in the name. On the given table the query returns 2 (MOHAN VERMA and RADHE MOHAN).Concept — pattern matching + counting
"Anywhere in the name" means an exact-match test (
=) will not do; we need theLIKEoperator with the%wildcard, which stands for any sequence of zero or more characters. The pattern'%MOHAN%'therefore matches a name whether 'MOHAN' is at the start, middle or end.COUNT(*)then counts the rows that survive theWHEREfilter — one row per matching driver.SELECT COUNT(*) FROM TRANSPORTER WHERE DRIVERNAME LIKE '%MOHAN%';Key lines:
WHERE DRIVERNAME LIKE '%MOHAN%'— keeps only rows whose DRIVERNAME contains the substring 'MOHAN'; the leading and trailing%allow any characters before and after it. In MySQL,LIKEon ordinary text columns is case-insensitive by default, so 'MOHAN' in the data matches as-is. …
- CBSE 2019Set 90/1/11 markQ.Consider the following table ‘Transporter’ that stores the order details about items to be transported. Table : TRANSPORTERWrite the output for the following SQL query :
ORDERNO DRIVERNAME DRIVERGRADE ITEM TRAVELDATE DESTINATION 10012 RAM YADAV A TELEVISION 2019-04-19 MUMBAI 10014 SOMNATH SINGH FURNITURE 2019-01-12 PUNE 10016 MOHAN VERMA B WASHING MACHINE 2019-06-06 LUCKNOW 10018 RISHI SINGH A REFRIGERATOR 2019-04-07 MUMBAI 10019 RADHE MOHAN TELEVISION 2019-05-30 UDAIPUR 10020 BISHEN PRATAP B REFRIGERATOR 2019-05-02 MUMBAI 10021 RAM TELEVISION 2019-05-03 PUNE (x) SELECT MAX(TRAVELDATE) FROM TRANSPORTER WHERE DRIVERGRADE = ‘A’;›Reveal solutionSolution
The query finds the most recent travel date among all orders handled by grade-A drivers. The result is 2019-04-19.
The
MAX()aggregate function returns the largest value in a column. When applied to dates, "largest" means the most recent date. TheWHEREclause filters the table to include only those rows where the driver's grade is exactly 'A', and thenMAX(TRAVELDATE)picks the latest date from that filtered subset.Let's trace through the logic step by step.
-
Filter by
DRIVERGRADE = 'A'The
WHEREclause restricts our view to only those rows where the driver has grade A. Scanning the table:ORDERNO DRIVERNAME DRIVERGRADE ITEM TRAVELDATE DESTINATION 10012 RAM YADAV A TELEVISION 2019-04-19 MUMBAI 10018 RISHI SINGH A REFRIGERATOR 2019-04-07 MUMBAI Only these two orders have
DRIVERGRADE = 'A'. Notice that rows withNULLor 'B' grades are excluded. -
Apply
MAX(TRAVELDATE)to the filtered rows …
-
🎓Unlock everything free for 14 days
- ✓Full step-by-step solutions
- ✓Concept-first explanations
- ✓Methods, shortcuts & mistakes
- ✓PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.