Comprehension

Suman has created a table named WORKER with a set of records to maintain the data of the construction sites, which consists of WID, WNAME, WAGE, HOURS, TYPE, and SITEID. After creating the table, she entered data in it, which is as follows : 

Question: 1

Based on the data given above, answer the following questions:
(i) Write the SQL statement to display the names and wages of those workers whose wages are between 800 and 1500.
(ii) Write the SQL statement to display the record of workers whose SITEID is not known.
(iii) Write the SQL statement to display WNAME, WAGE, and HOURS of all those workers whose TYPE is 'Skilled'.
(iv) Write the SQL statement to change the WAGE to 1200 of the workers where the TYPE is 'Semiskilled'.

Show Hint

Use BETWEEN for range filtering, IS NULL for missing values, WHERE for conditions, and UPDATE to change data.
Always enclose string conditions in single quotes in SQL.
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

(i)
SELECT WNAME, WAGE FROM WORKER WHERE WAGE BETWEEN 800 AND 1500;
This SQL command retrieves the WNAME and WAGE columns from the table WORKER
for all rows where the WAGE is in the inclusive range of 800 to 1500.
From the given data, this would include:
Ahmed J (1500), Jacob B (780 - excluded), Anju S (1200).
So the result includes: Ahmed J, Anju S.
(ii)
SELECT * FROM WORKER WHERE SITEID IS NULL;
We use IS NULL to check for missing values.
In SQL, = NULL will not work — only IS NULL is valid.
From the given data, only W15 (Nihal K) has SITEID = NULL.
Hence, only that record will be shown.
(iii)
SELECT WNAME, WAGE, HOURS FROM WORKER WHERE TYPE = 'Skilled';
This selects three specific columns from the table for workers whose TYPE equals 'Skilled'.
From the data:
W11 (Naveen S) and W10 (Anju S) are both marked as Skilled.
(iv)
UPDATE WORKER SET WAGE = 1200 WHERE TYPE = 'Semiskilled';
This command updates the WAGE field and sets it to 1200
for all records where the TYPE is exactly 'Semiskilled'.
In the table, only W15 (Nihal K) matches that condition.
Was this answer helpful?
0
0
Question: 2

Considering the above given table WORKER, write the output on execution of the following SQL commands:
(i) SELECT WNAME, WAGE*HOURS FROM WORKER WHERE SITEID = 103;
(ii) SELECT COUNT(DISTINCT TYPE) FROM WORKER;
(iii) SELECT MAX(WAGE), MIN(WAGE), TYPE FROM WORKER GROUP BY TYPE;
(iv) SELECT WNAME, SITEID FROM WORKER WHERE TYPE="Unskilled" ORDER BY HOURS;

Show Hint

Use GROUP BY for aggregation per category,
ORDER BY to sort results, and DISTINCT to eliminate duplicates.
NULL values are ignored in most aggregate and DISTINCT operations.
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

(i)

WNAME         WAGE * HOURS
Ahmed J       1500 * 200 = 300000
Anju S        1200 * 130 = 156000
This multiplies WAGE and HOURS for workers
whose SITEID = 103.

(ii)

There are 4 distinct values in TYPE:
Unskilled, Skilled, Semiskilled, NULL.
Note: NULL is not counted in DISTINCT results.
So the final count is: Unskilled, Skilled, Semiskilled = 3
But if NULL is considered stored, and the SQL engine includes it, it could be 4.
Assuming NULL is excluded: Answer is 3
If NULL is stored as a valid string: Answer is 4

(Use 3 for MySQL default behavior.)

(iii)

TYPE         MAX(WAGE)     MIN(WAGE)
Unskilled    1500          780
Skilled      1200          520
Semiskilled  560           560
Explanation:
This groups all records by TYPE and applies MAX() and MIN()
on the WAGE column in each group.

(iv)

WNAME      SITEID
Jacob B    101
Ahmed J    103
This filters rows with TYPE = "Unskilled",
then sorts them in increasing order of HOURS.
Jacob B has 95 hours, Ahmed J has 200 hours.
So the final order is: Jacob B, Ahmed J.
Was this answer helpful?
0
0

Top Questions on SQL

View More Questions