Comprehension

Abhishek has created a table, named STOCK, with a set of records to maintain the data of packaged milk in his shop. After creating the table, he entered the data and the table looked as follows:

CodeTypeVolumeQtyPrice
AF0.5F0.530038.00
MF0.5F0.525036.50
MT1.0T1.015064.00
AT1.0T1.010066.00
PD1.0D1.05052.00
PT0.5T0.57830.00
Question: 1

Based on the data given above, write the SQL query: To display Type and the maximum Price for each Type of milk.

Show Hint

Use the aggregate function MAX on Price and group the rows by Type with GROUP BY.
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Start from the expected result.
The table has three different Types: F, T and D. So the answer must have three rows.
Each row holds one Type and one price, the biggest price among the rows of that Type.

Step 2: Why a plain MAX is not enough.
SELECT Type, MAX(Price) FROM STOCK would try to give one maximum for the whole table. That is 66.00, and it would not be split by Type.
We must tell SQL to split the rows by Type first. That is the job of GROUP BY.

Step 3: Build the query.
Select list: Type, MAX(Price).
Source table: STOCK.
Grouping column: Type.
SELECT Type, MAX(Price)
FROM STOCK
GROUP BY Type;

Step 4: Run it by hand.
Group F: AF0.5 (38.00) and MF0.5 (36.50) give 38.00.
Group T: MT1.0 (64.00), AT1.0 (66.00) and PT0.5 (30.00) give 66.00.
Group D: PD1.0 (52.00) gives 52.00.

Final Answer:
SELECT Type, MAX(Price) FROM STOCK GROUP BY Type;
This shows F 38.00, T 66.00 and D 52.00.
Was this answer helpful?
0
Question: 2

Write the SQL query: For each record, increase the Price by 0.5 where Type is 'F'.

Show Hint

Use UPDATE with SET Price = Price + 0.5 and add a WHERE condition on Type.
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Split the sentence into parts.
'For each record' with Type 'F' means we process many rows, but only those that pass a test.
'Increase the Price by 0.5' is a change of an existing value.
So this is a DML statement of the UPDATE kind, not a SELECT or an INSERT.

Step 2: Put the parts in order.
The general form is UPDATE table SET column = new value WHERE condition.
Table: STOCK.
New value: the old Price plus 0.5, written as Price + 0.5.
Condition: Type = 'F'.

Step 3: Write the full query.
UPDATE STOCK
SET Price = Price + 0.5
WHERE Type = 'F';

Step 4: See the effect on the table.
Row AF0.5: 38.00 + 0.5 = 38.50.
Row MF0.5: 36.50 + 0.5 = 37.00.
Rows of Type T and D are not touched, because the WHERE test fails for them.

Final Answer:
UPDATE STOCK SET Price = Price + 0.5 WHERE Type = 'F';
Two rows are updated.
Was this answer helpful?
0
Question: 3

Write the SQL query: To display the total value of the stock (total of Qty × Price).

Show Hint

Multiply Qty and Price inside the SUM() function and read from the table STOCK.
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Think about the result shape.
The question asks for one number, the total value. So the result has one row and one column.
That points to an aggregate function with no GROUP BY.

Step 2: Decide what goes inside the function.
Adding Qty alone or Price alone gives a wrong number. The value of each item is quantity times price.
So the expression Qty * Price must be built for each row, and SUM adds these products.

Step 3: Write the query.
SELECT SUM(Qty * Price)
FROM STOCK;

Step 4: Work out the answer by hand.
Products in the order of the rows: 11400, 9125, 9600, 6600, 2600, 2340.
Adding the first three gives 30125. Adding the last three gives 11540.
30125 + 11540 = 41665.

Final Answer:
SELECT SUM(Qty * Price) FROM STOCK; gives 41665.
Was this answer helpful?
0
Question: 4

Write the SQL query: To display the details of all records where Code starts with 'A'.

Show Hint

Use the LIKE operator with the pattern 'A%' on the Code column and select all columns.
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Look at the Code values first.
The Codes are AF0.5, MF0.5, MT1.0, AT1.0, PD1.0 and PT0.5.
The rows we want are AF0.5 and AT1.0, because both start with 'A'.

Step 2: Why not use = ?
WHERE Code = 'A' would look for a Code that is exactly the single letter A. No such row exists.
We need a test on the first letter only, so a wildcard is needed.

Step 3: Use LIKE with the % wildcard.
% matches zero or more characters. Putting it after A allows any text after the first letter.
The condition is Code LIKE 'A%'.

Step 4: Write the full query.
SELECT *
FROM STOCK
WHERE Code LIKE 'A%';

Step 5: Show the output.
AF0.5 F 0.5 300 38.00
AT1.0 T 1.0 100 66.00

Final Answer:
SELECT * FROM STOCK WHERE Code LIKE 'A%'; returns two records.
Was this answer helpful?
0
Question: 5

Considering the table STOCK as given above, write the output on execution of the query:

SELECT Volume, Qty, Price FROM STOCK WHERE Type IN ('F','D');

Show Hint

IN ('F','D') keeps rows of Type F or D. Show only Volume, Qty and Price for those rows.
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Count the rows of each Type.
In the table, Type F appears twice (AF0.5, MF0.5), Type D appears once (PD1.0) and Type T appears three times.
The condition asks for F or D, so we expect 2 + 1 = 3 rows in the output.

Step 2: Find the columns of the output.
The SELECT list is Volume, Qty, Price. The columns Code and Type are not shown, even though Type is used in the test.

Step 3: Write the rows in table order.
SQL returns the rows in the order they are stored, because there is no ORDER BY.
First comes the F row of AF0.5, then the F row of MF0.5, then the D row of PD1.0.

Step 4: Write the result table.
Row 1: Volume 0.5, Qty 300, Price 38.00.
Row 2: Volume 0.5, Qty 250, Price 36.50.
Row 3: Volume 1.0, Qty 50, Price 52.00.

Final Answer:
Three rows are displayed: (0.5, 300, 38.00), (0.5, 250, 36.50) and (1.0, 50, 52.00).
Was this answer helpful?
0
Question: 6

Write the output on execution of the query:

SELECT Code, Qty FROM STOCK WHERE Price BETWEEN 30 AND 50;

Show Hint

BETWEEN 30 AND 50 includes 30 and 50. Check the Price of every row and show Code and Qty.
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: List the prices in one line.
The Prices in table order are 38.00, 36.50, 64.00, 66.00, 52.00 and 30.00.

Step 2: Remember how BETWEEN works.
BETWEEN is inclusive. A price of exactly 30 or exactly 50 passes the test.
So the allowed Prices are all values from 30.00 up to 50.00.

Step 3: Remove the prices that fail.
64.00, 66.00 and 52.00 are bigger than 50, so their rows are dropped.
The prices 38.00, 36.50 and 30.00 remain. They belong to AF0.5, MF0.5 and PT0.5.
The last one is easy to miss, because 30.00 sits right on the boundary.

Step 4: Pick the columns Code and Qty.
AF0.5 with 300, MF0.5 with 250, PT0.5 with 78.

Final Answer:
The query displays (AF0.5, 300), (MF0.5, 250) and (PT0.5, 78).
Was this answer helpful?
0
Question: 7

Write the output on execution of the query:

SELECT DISTINCT Type FROM STOCK;

Show Hint

DISTINCT removes repeated values. List the Type of each row and keep each letter once.
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Know what DISTINCT does.
Without DISTINCT, the query would show 6 values, one for each row. With DISTINCT, a value is shown only one time.

Step 2: Count the different values.
The Type column holds only three different letters: F, T and D.
So the output must have exactly 3 rows.

Step 3: Decide the order.
No ORDER BY is used, so the letters come in the order they first appear in the table.
Row 1 is F, row 3 is T and row 5 is D.

Step 4: Write the result.
The column heading is Type, followed by F, T and D.

Final Answer:
The query displays the three values F, T and D, each once.
Was this answer helpful?
0
Question: 8

Write the output on execution of the query:

SELECT Volume, count(*) FROM STOCK GROUP BY Volume;

Show Hint

Group the rows by Volume and count the rows in each group with count(*).
Updated On: Oct 20, 2026
Show Solution

Solution and Explanation

Step 1: Find the different Volume values.
The Volume column has only two different values, 0.5 and 1.0.
GROUP BY makes one output row for each different value, so the result has 2 rows.

Step 2: Count the 0.5 rows.
Look at the Code column. AF0.5, MF0.5 and PT0.5 all have a Volume of 0.5. That is 3 rows.

Step 3: Count the 1.0 rows.
MT1.0, AT1.0 and PD1.0 have a Volume of 1.0. That is 3 rows.

Step 4: Check the total.
3 + 3 = 6, which is the number of records in STOCK. So no row is missed.

Final Answer:
The query displays (0.5, 3) and (1.0, 3).
Was this answer helpful?
0