SQL Questions
SQL Questions
Write the query to create a table named 'employees' with fields like
empId, empName, empAge, empAddress, and empSalary.
Query:
2. empId INT,
3. empName VARCHAR(40),
4. empAge INT,
5. empAddress VARCHAR(40),
6. empSalary INT
7. );
Result: The 'employees' table will look like you can see below.
Query:
Result: After inserting values in the 'employees' table, it will look like you
can see below.
3. Consider the 'employees' table and write the query to delete record of
'empId=1' from the 'employees' table:
Query:
Result: The above statement will delete the record of 'empId=1' from the
'employees' table.
4. Consider the 'employees' table and write the query to update the age of
'empId=5' in the 'employees' table:
Query:
Result: The above statement will update the age of 'empId=5' in the
'employees' table.
5. Consider the 'employees' table and write the query to select all records
from the 'employees' table:
Query:
Result: As you can see below, above statement will select all records
from the 'employees' table.
6. Consider the 'employees' table and write the query to select the
'empName' and 'empSalary' columns from the 'employees' table:
Query:
Result: The above statement will select the 'empName' and 'empSalary'
columns from the 'employees' table.
empName empSalary
Daksh 32000
Ananya 37000
Aarush 35000
Reyansh 40000
Jhalak 39000
7. Consider the 'employees' table and write the query to select distinct
values from the 'empAddress' column in the 'employees' table:
Query:
Result: The above statement will select the distinct value from the
'empAddress' column in the 'employees' table.
empAddress
Jaipur
Bhopal
8. Consider the 'employees' table and write the query to filter and select
those records where empAddress='Jaipur' using a WHERE clause:
Query:
2. WHERE empAddress='Jaipur';
Result: The above statement will filter the records from the 'employees'
table.
9. Consider the 'employees' table and write the query to sort records of
the 'employees' table in descending order.
Query:
Result: The above statement will sort the records of the 'employees'
table in descending order.
10. Consider the 'employees' table and write the query to sort records of
the 'employees' table in ascending order.
Query:
Result: The above statement will sort the records of the 'employees'
table in ascending order.
Query:
Result: The above statement will count the number of rows in the
'employees' table.
COUNT(*)
12. Consider the 'employees' table and write the query to retrieve all
employees whose salary is between 35000 and 40000 in the 'employees'
table.
Query:
2. WHERE empSalary
Result: The above statement will retrieve all employees whose salary is
between 35000 and 40000 in the 'employees' table.
13. Consider the 'employees' table and write the query to add another
column called 'Department'.
Query:
1 Speaker 2 10000
2 Printer 1 21000
5 CD Drive 4 2000
Query:
1. SELECT SUM(product_price)
2. FROM products;
Result:
SUM(product_price)
37000
ii) Write the query to calculate the average value of the 'product_price'
column in the 'products' table.
Query:
1. SELECT AVG(product_price)
2. FROM products;
Result:
AVG(product_price)
7400
iii) Write the query to retrieve the minimum value from the 'product_price'
column in the 'products' table.
Query:
1. SELECT MIN(product_price)
2. FROM products;
Result:
MIN(product_price)
1500
iv) Write the query to retrieve the maximum value from a 'product_price'
column in the 'products' table.
Query:
1. SELECT MAX(product_price)
2. FROM products;
Result:
MAX(product_price)
21000
V) Write the query to group records and calculate aggregate functions for
the 'product_price' column in the 'products' table.
Query:
Result:
10000 1 10000
21000 1 21000
2500 1 2500
1500 1 1500
2000 1 2000
15. Consider a table called 'mobilephones' with fields such as Id, Name,
Company, Quantity, and Price.
Samsung
1 Samsung Blue 1 20000
Galaxy A23
iPhone 13
2 Apple Pink 2 65000
mini
Motorola
Viva
4 Edge 30 Motorola 2 38000
Magenta
Fusion
Samsung
5 Galaxy Z Samsung Black 4 48000
Flip3 5G
i) Write the query to select all customers from a 'Colour' column starting
with "bl" in the 'mobilephones' table.
Query:
Result:
10000 1 10000
21000 1 21000
2500 1 2500
1500 1 1500
2000 1 2000
15. Consider a table called 'mobilephones' with fields such as Id, Name,
Company, Quantity, and Price.
Samsung
1 Samsung Blue 1 20000
Galaxy A23
Samsung
5 Galaxy Z Samsung Black 4 48000
Flip3 5G
ii) Write the query to select all mobile names whose average is greater
than 45000 from the 'Price' column in the 'mobilephones' table.
Query:
2. FROM mobilephones
Result:
Name AVG(Price)
iPhone 12 54000
iii) Write the query to select all mobile names whose sum is smaller than
45000 from a 'Price' column in the 'mobilephones' table.
Query:
2. FROM mobilephones
Result:
Name SUM(Price)
i) Write the query to return the first non-null value from the firstName,
middleName, and lastName columns.
Query:
2. FROM customers;
Result:
Id Address NAME
1 Goa Shruti
2 Goa Raj
3 Goa Aadi
4 Mumbai Nishtha
5 Mumbai Kapoor
ii) Write the query to delete all rows from a 'customers' table, but keeps
the table structure.
Query:
Result: As you can see, the above statement has deleted all rows but left
with only the table structure.
17. Consider the two tables: one is the 'teachers' table which has fields
such as teachers_id, Name, teachers_age, and teachers_address. The
other is the 'students' table which has fields such as students_id, Name,
students_age, and students_address.
i) Write the query to combine the 'Name' column in the 'teachers' table
with the 'Name' column in the 'students' table using the UNION operator.
Query:
Result:
Name
Karishma
Yasha
Kartik
Milan
Harsh
Palak
Himanshi
Ansh
ii) Write the query to find the teacher's name whose age is between 34
and 41 in the 'teachers' table.
Query:
2. WHERE teachers_age
Result: