Q.Write outputs for SQL queries
🔒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 Query Concepts
SQL Query Concepts — A First Look
Think of a library. You walk in, and instead of wandering through endless shelves, you go to the librarian and say: "Give me all the books by Tagore published after 1950, sorted by title." The librarian knows exactly where everything is, finds what you asked for, and hands you the list. That's what SQL does — but for data stored in a computer.
SQL stands for Structured Query Language. It is the language we use to talk to databases. A database is just a collection of related information stored in tables — like an Excel workbook with multiple sheets, but far more powerful and organised.
The core idea: tables and queries
A database stores data in tables. Each table has rows (records) and columns (fields). For example, a Students table might have columns like RollNo, Name, City, and Marks. Each row is one student's complete information.
A query is simply a question you ask the database. You write it in SQL, and the database returns an answer — usually a new table made from the data that matches your question.
SQL is not a programming language like Python or Java. You don't tell the computer how to find the data. You only say what data you want. The database figures out the fastest way to get it.
The most common SQL command: SELECT
Almost every query begins with SELECT. It means "show me this data." The basic structure is:
SELECT column1, column2 FROM table_name WHERE condition;
Read it like a sentence: "Select these columns from this table where this condition is true."
If you want all columns, you write SELECT *. The asterisk is a shortcut meaning "everything."
Filtering with WHERE
The WHERE clause is your filter. It keeps only the rows that satisfy a condition. For example, if you want students from Delhi only, you write:
SELECT * FROM Students WHERE City = 'Delhi';
You can combine conditions using AND (both must be true) and OR (at least one must be true). This is exactly like everyday logic: "Show me students from Delhi AND who scored above 80."
Sorting with ORDER BY
Data often comes back in no particular order. To arrange it, use ORDER BY. You can sort ascending (smallest first, which is the default) or descending (largest first) using DESC.
SELECT Name, Marks FROM Students ORDER BY Marks DESC;
This gives you a ranked list — highest marks first.
Why SQL matters for commerce and humanities
You will never calculate a formula in SQL. That is not what it does. What SQL does is organise, filter, and retrieve information — and that is the foundation of every data-driven decision. …
Let us analyze each SQL query to understand its purpose and determine the resulting output based on the provided CUSTOMERS and PURCHASES tables.
(i) SELECT COUNT(DISTINCT CITIES) FROM CUSTOMERS;
This query counts the number of unique city names present in the CITIES column of the CUSTOMERS table. The DISTINCT keyword ensures that each city is counted only once, even if multiple customers are from the same city. The unique cities listed are Delhi, Mumbai, Chennai, Indore, and Bangalore.
Output:
| COUNT(DISTINCT CITIES) |
|---|
| 5 |
(ii) SELECT MAX(PUR_DATE) FROM PURCHASES;
This query retrieves the latest purchase date from the PUR_DATE column in the PURCHASES table. The MAX aggregate function identifies the chronologically highest date among all entries.
Output:
| MAX(PUR_DATE) |
|---|
| 2019-05-09 |
(iii) SELECT CNAME, QTY, PUR_DATE FROM CUSTOMERS, PURCHASES WHERE CUSTOMERS.CNO = PURCHASES.CNO AND QTY IN (10,20); …
The queries count distinct cities, find the latest purchase date, and list customer names, quantities, and purchase dates for specific quantities.
In database management, understanding the structure of tables and how they relate is fundamental to extracting meaningful information. Each table typically has a Primary Key, a column (or set of columns) that uniquely identifies each record. This key ensures data integrity and allows for efficient retrieval and linking of information across different tables.
For instance, in the CUSTOMERS table, CNO (Customer Number) serves as the Primary Key. Each customer has a unique CNO, ensuring that we can distinguish between SANYAM (C1) and SHRUTI (C2), even if they share the same city. This CNO then appears in the PURCHASES table as a Foreign Key, linking each purchase record back to the specific customer who made it. This relationship is crucial for queries that involve data from both tables, such as finding out which customer made a particular purchase.
A Primary Key uniquely identifies each record in a table, while a Foreign Key establishes a link between two tables by referencing the Primary Key of another table. This relational structure is the backbone of relational databases.
Let's now examine the specific SQL queries and their outputs, understanding the logic behind each operation.
Query (i): SELECT COUNT(DISTINCT CITIES) FROM CUSTOMERS;
This query aims to determine the number of unique cities from which our customers originate. The DISTINCT keyword is vital here; without it, the query would simply count every entry in the CITIES column, including duplicates. By specifying DISTINCT CITIES, we instruct the database to consider only the unique city names before counting them.
Looking at the CUSTOMERS table:
- DELHI appears multiple times (for C1, C2, C6).
- MUMBAI appears twice (for C3, C9).
- CHENNAI appears twice (for C4, C7).
- INDORE appears once (for C5).
- BANGALORE appears once (for C8).
The unique cities are DELHI, MUMBAI, CHENNAI, INDORE, and BANGALORE. Counting these distinct entries gives us the total number of unique cities.
Output for Query (i):
| COUNT(DISTINCT CITIES) |
|---|
| 5 |
Query (ii): SELECT MAX(PUR_DATE) FROM PURCHASES;
This query seeks to identify the latest date on which a purchase was made. The MAX() aggregate function is used to find the highest value within a specified column. For date columns, MAX() returns the most recent date.
Examining the PUR_DATE column in the PURCHASES table:
- 2018-12-25
- 2018-11-10
- 2018-11-10
- 2019-01-12
- 2019-02-12
- 2018-10-12
- 2019-05-09
- 2019-05-09
- 2018-05-09
- 2018-11-12
- 2018-08-04
By comparing all these dates, the latest date recorded is 2019-05-09.
Output for Query (ii):
| MAX(PUR_DATE) |
|---|
| 2019-05-09 |
Query (iii): SELECT CNAME, QTY, PUR_DATE FROM CUSTOMERS, PURCHASES WHERE CUSTOMERS.CNO = PURCHASES.CNO AND QTY IN (10,20);
This query is more complex, involving a join between two tables and a filtering condition. …
Showing the 12 most recent of 13 on this concept.
- CBSE 2025Set 90/1/11 markMCQQ.With respect to SQL, match the function given in column-II with categories given in column-I: Column-I:(i) Math function(ii) Aggregate function(iii) Date function(iv) Text function Column-II:(a) COUNT()(b) ROUND()(c) RIGHT()(d) YEAR() Options: (A) (i)-(c), (ii)-(d), (iii)-(a), (iv)-(b) (B) (i)-(b), (ii)-(a), (iii)-(d), (iv)-(c) (C) (i)-(d), (ii)-(b), (iii)-(a), (iv)-(c) (D) (i)-(b), (ii)-(c), (iii)-(d), (iv)-(a)
›Reveal solutionSolution
SQL functions fall into distinct categories based on their purpose: Math functions perform numerical calculations, Aggregate functions summarize data across rows, Date functions manipulate temporal values, and Text functions handle string operations.
SQL organizes its built-in functions into logical families, each designed to solve a particular class of problem. Understanding these categories isn't just about memorization—it's about recognizing what kind of operation you need when writing queries.
Math functions work with numerical data to perform calculations on individual values. ROUND() is the quintessential example here: it takes a number and rounds it to a specified number of decimal places. When you need to clean up floating-point results or present currency values neatly, ROUND() does the arithmetic work. It operates on one number at a time, transforming it according to mathematical rules.
Aggregate functions tell a different story—they collapse many rows into a single summary value. COUNT() epitomizes this category. Instead of transforming individual values, it looks across an entire set of rows (or a grouped subset) and returns how many there are. Whether you're counting customers, transactions, or inventory items, COUNT() synthesizes information from multiple records into one meaningful number. Other members of this family include SUM(), AVG(), MAX(), and MIN(), all sharing that same "many-to-one" characteristic.
Date functions specialize in extracting or manipulating components of temporal data. YEAR() is a classic representative: given a date or datetime value, it pulls out just the year portion as an integer. This becomes invaluable when you need to filter records by year, group sales by annual periods, or calculate ages. Date functions understand the peculiarities of calendars—leap years, month lengths, time zones—so you don't have to parse dates manually. …
- CBSE 2024Set 90/1/11 markMCQQ.Which of the following is not an aggregate function in MYSQL ? (A) AVG() (B) MAX() (C) LCASE() (D) MIN()
›Reveal solutionSolution
LCASE() is a string manipulation function that converts text to lowercase, not an aggregate function that performs calculations across multiple rows.
Aggregate functions in SQL are designed to perform a calculation 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 "What's the average?" or "What's the highest value?" across an entire dataset or group.
The three functions AVG(), MAX(), and MIN() are classic examples of aggregate functions. AVG() computes the arithmetic mean of a numeric column — add up all the values and divide by the count. MAX() finds the largest value in a set, whether that's the highest salary, the most recent date, or even the last string alphabetically. MIN() does the opposite, identifying the smallest or earliest value. All three operate on multiple rows and condense them into a single output.
LCASE(), on the other hand, belongs to an entirely different category: string functions. It takes a single text value and converts all its characters to lowercase. If you pass it "HELLO", it returns "hello". This is a row-by-row transformation — it doesn't summarize or aggregate anything. Each input row gets its own output; there's no calculation across multiple rows. …
- CBSE 2024Set 90/1/11 markMCQQ.Which MySQL command helps to add a primary key constraint to any table that has already been created ? (A) UPDATE (B) INSERT INTO (C) ALTER TABLE (D) ORDER BY
›Reveal solutionSolution
Primary key constraints on existing tables are added through schema modification commands. The answer is (C) ALTER TABLE.
When you create a table in MySQL, you define its structure: column names, data types, and constraints like primary keys. But what happens when you've already created a table and later realize you need to add a primary key? You need a command that modifies the table's structure itself, not its data.
This is where the distinction between Data Definition Language (DDL) and Data Manipulation Language (DML) becomes crucial. DDL commands change the schema—the blueprint of your database objects. DML commands work with the data inside those objects.
Let's examine each option:
-
UPDATE is a DML command that modifies existing data in rows. You use it to change values in columns, like
UPDATE students SET grade = 'A' WHERE id = 5. It cannot alter the table's structure or add constraints. -
INSERT INTO is another DML command that adds new rows of data to a table. It populates the table with records but has no capability to modify the table definition itself.
-
ALTER TABLE is the DDL command specifically designed to modify an existing table's structure. You can add columns, drop columns, change data types, and crucially, add or remove constraints like primary keys, foreign keys, and unique constraints. …
-
- CBSE 2024Set 90/1/11 markMCQQ.Which of the following clause cannot work with SELECT statement in MYSQL ? (A) FROM (B) INSERT INTO (C) WHERE (D) GROUP BY
›Reveal solutionSolution
INSERT INTO is a separate DML statement for adding rows, not a clause that works with SELECT. The answer is (B).
Understanding SQL Clauses vs. Statements
SQL distinguishes between statements (complete commands) and clauses (components that modify or extend a statement). A SELECT statement retrieves data and can be enhanced with various clauses that filter, group, or specify the source of that data.
The question asks which option cannot work with a SELECT statement. Let's examine each:
-
FROM clause – Specifies the table(s) from which to retrieve data. Every practical SELECT needs a FROM clause (except trivial cases like
SELECT 1;). This is fundamental to SELECT.SELECT name FROM students; -
WHERE clause – Filters rows based on conditions. This works seamlessly with SELECT to narrow down results.
SELECT name FROM students WHERE age > 18; -
GROUP BY clause – Groups rows sharing common values, typically used with aggregate functions. This is a standard SELECT clause.
SELECT department, COUNT(*) FROM employees GROUP BY department; -
INSERT INTO – This is not a clause; it's a completely separate DML statement used to add new rows to a table. You cannot "attach" INSERT INTO to a SELECT statement as you would a clause. …
-
- CBSE 2023Set 91/11 markMCQQ.Fill in the blank: .............. clause is used with SELECT statement to display data in a sorted form with respect to a specified column.(a) WHERE(b) ORDER BY(c) HAVING(d) DISTINCT
›Reveal solutionSolution
The ORDER BY clause sorts query results by one or more columns in ascending or descending order.
When you retrieve data from a database table using a SELECT statement, the rows typically appear in no particular order—often the sequence in which they were inserted, though even that isn't guaranteed. If you want your results organized meaningfully, you need a way to impose order on the output.
The ORDER BY clause exists precisely for this purpose. It tells the database management system to arrange the result set according to the values in one or more specified columns. By default, ORDER BY sorts in ascending order (smallest to largest for numbers, A to Z for text), but you can explicitly request descending order using the DESC keyword.
Consider a simple example: if you have a student table with columns for name, age, and marks, writing
SELECT * FROM students ORDER BY markswould display all students sorted from lowest to highest marks. AddingORDER BY marks DESCwould reverse that, showing top scorers first.Let's quickly examine why the other options don't fit:
- WHERE filters rows before they're selected, keeping only those that meet a condition (like
WHERE age > 15) - HAVING filters groups after aggregation, used with GROUP BY to filter summarized data …
- WHERE filters rows before they're selected, keeping only those that meet a condition (like
- CBSE 2023Set 90/1/11 markMCQQ.Aggregate functions are also known as: (A) Scalar Functions (B) Single Row Functions (C) Multiple Row Functions (D) Hybrid Functions
›Reveal solutionSolution
Aggregate functions are called Multiple Row Functions because they operate on groups of rows and return a single result per group.
When you're working with databases and SQL, you quickly discover that not all functions behave the same way. Some functions work on individual values—one row at a time—while others need to look at many rows together to produce a meaningful result. Understanding this distinction is fundamental to writing effective queries.
Aggregate functions belong to the second category. They take multiple rows of data as input and collapse them into a single summary value. Think about what happens when you use
SUM(),COUNT(),AVG(),MAX(), orMIN(). Each of these functions examines an entire set of rows—perhaps all the sales in a month, or all the students in a class—and produces one answer. You're asking the database to aggregate information across many records, which is exactly why they're called aggregate functions.The terminology "Multiple Row Functions" captures this behavior perfectly. These functions process multiple rows simultaneously and return one result for the entire group. If you have a table with a thousand employee records and you write
SELECT AVG(salary) FROM employees, theAVG()function reads all thousand salary values and gives you back a single average. That's multiple-row processing in action.NoteThe term "Multiple Row Functions" emphasizes the input side of the operation—these functions consume many rows. The term "aggregate" emphasizes what they do with those rows—they combine or summarize them. …
- CBSE 2023Set 90/1/11 markMCQQ.Ravisha has stored the records of all students of her class in a MYSQL table. Suggest a suitable SQL clause that she should use to display the names of students in alphabetical order. (A) SORT BY (B) ALIGN BY (C) GROUP BY (D) ORDER BY
›Reveal solutionSolution
To display student names in alphabetical order, Ravisha should use the
ORDER BYclause in her SQL query.When working with databases, especially with a large number of records like student information, simply retrieving data isn't always enough. Often, we need to present this data in a structured and readable manner. One of the most common requirements is to sort the data based on certain criteria, such as displaying names alphabetically, scores from highest to lowest, or dates chronologically. This is where SQL clauses designed for ordering come into play.
The Structured Query Language (SQL) provides specific commands to manipulate and retrieve data from relational databases. To achieve the task of displaying student names in alphabetical order, Ravisha needs a clause that explicitly tells the database system to arrange the retrieved records based on the values in the 'name' column.
The correct SQL clause for sorting a result set is
ORDER BY. This clause allows you to specify one or more columns by which the data should be sorted. By default,ORDER BYsorts data in ascending order (A-Z for text, smallest to largest for numbers, earliest to latest for dates). If you want to sort in descending order, you explicitly use theDESCkeyword after the column name. For alphabetical order, the default ascending sort is exactly what is needed.For example, if Ravisha's table is named
Studentsand it has a columnStudentName, a query to display names alphabetically would conceptually look like:SELECT StudentName FROM Students ORDER BY StudentName;ImportantThe
ORDER BYclause is always applied to the result set of aSELECTstatement and determines the final presentation order of the rows.Let's briefly consider why the other options are not suitable:
- SORT BY: This is not a standard SQL clause. While the concept of sorting is what we're after,
SORT BYis not the correct syntax in MySQL or other standard SQL databases. - ALIGN BY: This is also not a recognized SQL clause for sorting or any other standard database operation. It might be confused with formatting or display options in specific reporting tools, but not for SQL itself. …
- SORT BY: This is not a standard SQL clause. While the concept of sorting is what we're after,
- CBSE 2019Set 90/1/11 markQ.‘Employee’ table has a column named ‘CITY’ that stores city in which each employee resides. Write SQL query to display details of all rows except those rows that have CITY as ‘DELHI’ or ‘MUMBAI’ or ‘CHANDIGARH’.
›Reveal solutionSolution
To exclude rows where CITY is DELHI, MUMBAI, or CHANDIGARH, use the
NOT INoperator with a list of those city names in the WHERE clause.When you need to filter out specific values from a column, SQL gives you a clean and readable way to do it. The
NOT INoperator is exactly what you need here — it lets you specify a list of values to exclude, and the database returns every row where the column does not match any value in that list.Think of it this way: you want all employees except those living in three particular cities. Instead of writing multiple
ANDconditions with!=(which would be messy and error-prone), you simply writeCITY NOT IN ('DELHI', 'MUMBAI', 'CHANDIGARH'). The database checks each row: if the CITY value is any of those three, the row is skipped; otherwise, it is included.NoteThe
NOT INoperator works with any data type — numbers, dates, or text. Just ensure the values in the list match the column's data type exactly (here, text strings in single quotes).The full query would look like this:
SELECT * FROM Employee WHERE CITY NOT IN ('DELHI', 'MUMBAI', 'CHANDIGARH');This returns all columns (
*) for every employee whose city is not Delhi, Mumbai, or Chandigarh. If you only need specific columns (like name, salary, or department), replace*with those column names. … - 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 (i) To display names of drivers and destination city where TELEVISION is being transported.›Reveal solutionSolution
We need to filter rows where the ITEM is 'TELEVISION' and display only the driver's name and destination city from those matching records.
When working with a database table, the fundamental task is to retrieve specific information based on conditions. The TRANSPORTER table holds a complete record of transport orders, but we rarely need all columns or all rows at once. SQL's SELECT statement lets us pick exactly what we want to see and which records to include.
The question asks for two pieces of information: the names of drivers and the cities they're heading to, but only when they're transporting televisions. This is a classic filtering problem. We need to scan through the table, identify rows where the ITEM column contains 'TELEVISION', and then pull out just the DRIVERNAME and DESTINATION columns from those rows.
Looking at the table, three orders involve televisions: order 10012 (RAM YADAV to MUMBAI), order 10019 (RADHE MOHAN to UDAIPUR), and order 10021 (RAM to PUNE). Our query should return exactly these three driver-destination pairs.
The structure of the SQL command follows a simple pattern: we SELECT the columns we want, FROM the table that holds them, WHERE a condition is true. The condition here is straightforward—ITEM must equal 'TELEVISION'. In SQL, text values are enclosed in single quotes, and the equality operator is a simple equals sign.
NoteSQL is case-insensitive for keywords (SELECT, FROM, WHERE), but it's good practice to write them in uppercase for readability. Column names and table names follow the case sensitivity rules of your database system, though most treat them as case-insensitive. String values in the WHERE clause, however, are case-sensitive by default in many databases. …
- 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 (ii) To display driver names and destinations where destination is not MUMBAI.›Reveal solutionSolution
To display driver names and destinations where the destination is not Mumbai, you use the
SELECTcommand with aWHEREclause that excludes 'MUMBAI' using the<>or!=operator.The table you're working with is called Transporter, and it holds records of orders for transporting items. Each row tells you who the driver is, what grade they hold, which item they're carrying, when the travel happened, and where they're headed. This is a classic example of a relational database table — a structured way to store and retrieve information.
When you need to pull out specific columns from a table, you use the
SELECTstatement. In this case, you want only two columns:DRIVERNAMEandDESTINATION. But you don't want all destinations — you want to filter out Mumbai. That's where theWHEREclause comes in. It acts like a sieve, letting through only those rows that meet a condition.The condition here is "destination is not MUMBAI". In SQL, "not equal to" is written as
<>(or sometimes!=in some database systems). So the condition becomesDESTINATION <> 'MUMBAI'. Notice that the text value 'MUMBAI' is enclosed in single quotes — that's how SQL distinguishes a literal string from a column name or keyword.NoteSQL is case-insensitive for keywords like
SELECTandWHERE, but the data inside quotes is case-sensitive in many systems. So'MUMBAI'will match exactly that spelling. If the table had 'Mumbai' or 'mumbai', it might not match — always check the actual data.Now, let's look at the table. The rows where destination is not Mumbai are:
- Order 10014: Somnath Singh → Pune
- Order 10016: Mohan Verma → Lucknow
- Order 10019: Radhe Mohan → Udaipur
- Order 10021: Ram → Pune …
- 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 (iii) To display the names of destination cities where items are being transported. There should be no duplicate values.›Reveal solutionSolution
To display unique destination cities, use the
SELECT DISTINCTcommand on theDESTINATIONcolumn of theTRANSPORTERtable.When working with databases, we often encounter situations where a particular piece of information, like a city name, might appear multiple times across different records. For instance, in the
TRANSPORTERtable, many orders might be destined for 'MUMBAI'. If our goal is simply to list all the different cities to which items are transported, rather than every single instance of a city for each order, we need a way to eliminate these repeated entries. This is where the concept of retrieving unique values becomes crucial.The SQL
DISTINCTkeyword serves precisely this purpose. When placed immediately afterSELECTand before the column name, it instructs the database management system to return only the unique values from that specified column in the result set. Any duplicate entries for that column will be filtered out, ensuring that each value appears only once. This is particularly useful for generating lists of categories, types, or, in this case, unique destinations, without being overwhelmed by redundant information. … - 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 (iv) To display details of rows that have some value in DRIVERGRADE column.›Reveal solutionSolution
"Some value in DRIVERGRADE" means the column is NOT NULL — use
SELECT * FROM TRANSPORTER WHERE DRIVERGRADE IS NOT NULL;. It returns the 4 rows whose grade is A or B.Concept — NULL means "no value", and it needs special syntax
In the TRANSPORTER table three rows (SOMNATH SINGH, RADHE MOHAN, RAM) have an empty DRIVERGRADE — in SQL these are NULL entries: the absence of a value, distinct from
0or''. NULL cannot be tested with ordinary comparison operators:DRIVERGRADE = NULLorDRIVERGRADE <> NULLnever matches any row, because any comparison with NULL evaluates to UNKNOWN. SQL provides dedicated predicates instead:IS NULLandIS NOT NULL.The question asks for rows that have some value in DRIVERGRADE, i.e. rows where the column is not NULL.
The SQL command
SELECT * FROM TRANSPORTER WHERE DRIVERGRADE IS NOT NULL;Expected output on the given data
ORDERNO DRIVERNAME DRIVERGRADE ITEM TRAVELDATE DESTINATION 10012 RAM YADAV A TELEVISION 2019-04-19 MUMBAI 10016 MOHAN VERMA B WASHING MACHINE 2019-06-06 LUCKNOW 10018 RISHI SINGH A REFRIGERATOR 2019-04-07 MUMBAI
🎓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.