0% found this document useful (0 votes)
4 views18 pages

Computer Science Worksheet On Simple Queris in MySQL

Uploaded by

blackdiman050
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views18 pages

Computer Science Worksheet On Simple Queris in MySQL

Uploaded by

blackdiman050
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Worksheet: Simple Queries in MySQL (Class XII CBSE Computer Science)

Section A: Multiple Choice Questions (MCQs) - 30 Questions

1. Which MySQL command is used to retrieve data from a table?


a) INSERT
b) SELECT
c) UPDATE
d) DELETE
Answer: b) SELECT

2. What does the WHERE clause do in a MySQL query?


a) Joins multiple tables
b) Filters rows based on a condition
c) Groups rows by a column
d) Orders the result set
Answer: b) Filters rows based on a condition

3. Which keyword is used to sort query results in ascending order?


a) SORT BY
b) ORDER BY
c) GROUP BY
d) ARRANGE BY
Answer: b) ORDER BY

4. What is the purpose of the GROUP BY clause?


a) To filter rows
b) To group rows with the same values into summary rows
c) To join tables
d) To delete duplicate rows
Answer: b) To group rows with the same values into summary rows

5. Which function returns the total number of rows in a result set?


a) SUM()
b) COUNT()
c) AVG()
d) MAX()
Answer: b) COUNT()

6. What does the DISTINCT keyword do in a SELECT query?


a) Removes duplicate rows from the result
b) Sorts the result set
c) Groups rows by a column
d) Joins tables
Answer: a) Removes duplicate rows from the result

7. Which operator is used to match a pattern in a string?


a) IN
b) LIKE
c) BETWEEN
d) IS NULL
Answer: b) LIKE

8. What is the result of the query: SELECT * FROM students WHERE age > 15;?
a) All columns for students with age less than 15
b) All columns for students with age greater than 15
c) All columns for students with age equal to 15
d) All columns for all students
Answer: b) All columns for students with age greater than 15

9. Which join returns all rows from both tables, with NULLs in places where there is no match?
a) INNER JOIN
b) LEFT JOIN
c) RIGHT JOIN
d) FULL OUTER JOIN
Answer: d) FULL OUTER JOIN

10. What does the HAVING clause do?


a) Filters rows before grouping
b) Filters groups after GROUP BY
c) Joins tables
d) Orders the result set
Answer: b) Filters groups after GROUP BY

11. Which function calculates the average of a column?


a) SUM()
b) AVG()
c) COUNT()
d) MIN()
Answer: b) AVG()

12. What is the default sorting order of the ORDER BY clause?


a) Ascending
b) Descending
c) Random
d) None
Answer: a) Ascending

13. Which clause is used to combine rows from two or more tables based on a related column?
a) WHERE
b) JOIN
c) GROUP BY
d) ORDER BY
Answer: b) JOIN

14. What does the query SELECT COUNT(*) FROM employees; return?
a) Sum of all salaries
b) Total number of rows in the employees table
c) Maximum salary
d) Average salary
Answer: b) Total number of rows in the employees table

15. Which operator checks if a value lies within a range?


a) IN
b) BETWEEN
c) LIKE
d) IS NULL
Answer: b) BETWEEN

16. What is the purpose of the AS keyword in a query?


a) To filter rows
b) To rename a column or table
c) To group rows
d) To join tables
Answer: b) To rename a column or table

17. Which query retrieves unique department names from an employees table?
a) SELECT department FROM employees;
b) SELECT DISTINCT department FROM employees;
c) SELECT UNIQUE department FROM employees;
d) SELECT department FROM employees GROUP BY department;
Answer: b) SELECT DISTINCT department FROM employees;

18. What does the INNER JOIN return?


a) All rows from both tables
b) Only matching rows from both tables
c) All rows from the left table
d) All rows from the right table
Answer: b) Only matching rows from both tables

19. Which function returns the largest value in a column?


a) MAX()
b) MIN()
c) SUM()
d) COUNT()
Answer: a) MAX()

20. What is the result of SELECT * FROM products WHERE price IS NULL;?
a) All products with a price of 0
b) All products with no price value
c) All products with a price greater than 0
d) All products
Answer: b) All products with no price value

21. Which clause is used to limit the number of rows returned?


a) WHERE
b) LIMIT
c) GROUP BY
d) ORDER BY
Answer: b) LIMIT

22. What does the % wildcard represent in a LIKE clause?


a) A single character
b) Zero or more characters
c) A specific character
d) A number
Answer: b) Zero or more characters

23. Which query retrieves employees with salaries between 50000 and 70000?
a) SELECT * FROM employees WHERE salary IN (50000, 70000);
b) SELECT * FROM employees WHERE salary BETWEEN 50000 AND 70000;
c) SELECT * FROM employees WHERE salary > 50000;
d) SELECT * FROM employees WHERE salary < 70000;
Answer: b) SELECT * FROM employees WHERE salary BETWEEN 50000 AND 70000;

24. What does the MIN() function do?


a) Returns the smallest value in a column
b) Returns the average value
c) Returns the total number of rows
d) Returns the sum of values
Answer: a) Returns the smallest value in a column

25. Which query groups employees by department and counts them?


a) SELECT department, COUNT() FROM employees;
b) SELECT department, COUNT() FROM employees GROUP BY department;
c) SELECT department FROM employees GROUP BY department;
d) SELECT COUNT() FROM employees ORDER BY department;
Answer: b) SELECT department, COUNT() FROM employees GROUP BY department;

26. What is the purpose of the AND operator in a WHERE clause?


a) Combines multiple conditions, all must be true
b) Combines multiple conditions, at least one must be true
c) Excludes conditions
d) Joins tables
Answer: a) Combines multiple conditions, all must be true

27. Which query retrieves the top 5 highest-paid employees?


a) SELECT * FROM employees ORDER BY salary DESC LIMIT 5;
b) SELECT * FROM employees ORDER BY salary LIMIT 5;
c) SELECT TOP 5 * FROM employees;
d) SELECT * FROM employees WHERE salary > 5;
Answer: a) SELECT * FROM employees ORDER BY salary DESC LIMIT 5;

28. What does the OR operator do in a WHERE clause?


a) Combines conditions, all must be true
b) Combines conditions, at least one must be true
c) Excludes conditions
d) Groups rows
Answer: b) Combines conditions, at least one must be true

29. Which query joins two tables, orders and customers, on customer ID?
a) SELECT * FROM orders JOIN customers WHERE orders.cid = customers.cid;
b) SELECT * FROM orders INNER JOIN customers ON orders.cid = customers.cid;
c) SELECT * FROM orders, customers WHERE orders.cid = customers.cid;
d) Both b and c
Answer: b) SELECT * FROM orders INNER JOIN customers ON orders.cid = customers.cid;

30. What is the output of SELECT SUM(price) FROM products;?


a) Total number of products
b) Total price of all products
c) Average price of products
d) Maximum price of a product
Answer: b) Total price of all products

Section B: Assertion-Reason Questions - 30 Questions

Instructions: For each question, two statements are given: Assertion (A) and Reason (R). Choose the
correct option:
a) Both A and R are true, and R is the correct explanation of A.
b) Both A and R are true, but R is not the correct explanation of A.
c) A is true, but R is false.
d) A is false, but R is true.

1. A: The SELECT statement retrieves data from a table.


R: It can be used with clauses like WHERE, GROUP BY, and ORDER BY to refine results.
Answer: a)

2. A: The WHERE clause filters rows after the GROUP BY clause.


R: The WHERE clause is applied before grouping.
Answer: d)

3. A: The ORDER BY clause sorts results in ascending order by default.


R: You can use DESC to sort in descending order.
Answer: a)

4. A: The COUNT(*) function counts only non-NULL values.


R: The COUNT(*) function counts all rows, including NULLs.
Answer: d)

5. A: The LIKE operator is used for pattern matching in strings.


R: The % wildcard matches zero or more characters.
Answer: a)

6. A: The INNER JOIN returns only matching rows from both tables.
R: It excludes rows with no match in either table.
Answer: a)
7. A: The GROUP BY clause is used to filter individual rows.
R: The GROUP BY clause groups rows with the same values for summary.
Answer: d)

8. A: The HAVING clause filters groups after GROUP BY.


R: It is similar to the WHERE clause but applied after grouping.
Answer: a)

9. A: The DISTINCT keyword removes duplicate rows from the result set.
R: It can be used with any column in a SELECT query.
Answer: a)

10. A: The BETWEEN operator includes the boundary values.


R: It is equivalent to using >= and <= operators.
Answer: a)

11. A: The LEFT JOIN returns all rows from the left table.
R: It includes NULLs for non-matching rows from the right table.
Answer: a)

12. A: The SUM() function can only be used with numeric columns.
R: It calculates the total of values in a column.
Answer: a)

13. A: The LIMIT clause is used to restrict the number of rows returned.
R: It is always used with the ORDER BY clause.
Answer: c)

14. A: The AS keyword renames a column or table in the result set.


R: It improves readability of the query output.
Answer: a)

15. A: The FULL OUTER JOIN is supported in all MySQL versions.


R: MySQL does not natively support FULL OUTER JOIN.
Answer: d)

16. A: The MAX() function returns the largest value in a column.


R: It can be used with numeric, string, or date columns.
Answer: a)

17. A: The IN operator checks if a value matches any value in a list.


R: It is faster than using multiple OR conditions.
Answer: a)

18. A: The WHERE clause can be used with aggregate functions.


R: The HAVING clause is used to filter aggregate functions.
Answer: d)

19. A: The RIGHT JOIN returns all rows from the right table.
R: It includes NULLs for non-matching rows from the left table.
Answer: a)
20. A: The AVG() function ignores NULL values.
R: It calculates the average of non-NULL values in a column.
Answer: a)

21. A: The ORDER BY clause can sort by multiple columns.


R: Each column can have its own sorting order (ASC or DESC).
Answer: a)

22. A: The SELECT * retrieves all columns from a table.


R: It is not recommended for large tables due to performance issues.
Answer: a)

23. A: The AND operator combines conditions where all must be true.
R: It has higher precedence than the OR operator.
Answer: a)

24. A: The MIN() function can be used with string columns.


R: It returns the smallest value based on alphabetical order for strings.
Answer: a)

25. A: The GROUP BY clause must include all non-aggregate columns in the SELECT statement.
R: This ensures the query is logically consistent.
Answer: a)

26. A: The IS NULL operator checks for missing values.


R: It is used to filter rows with NULL values in a column.
Answer: a)

27. A: The JOIN clause is mandatory to combine data from multiple tables.
R: You can use a WHERE clause to join tables without using JOIN.
Answer: b)

28. A: The COUNT(DISTINCT column) counts unique non-NULL values.


R: It ignores duplicate values in the specified column.
Answer: a)

29. A: The LIKE operator is case-sensitive in MySQL by default.


R: You can use LOWER() or UPPER() to make it case-insensitive.
Answer: a)

30. A: The HAVING clause can only be used with GROUP BY.
R: It filters groups based on aggregate conditions.
Answer: a)

Section C: Table-Based Questions - 50 Questions

Database Schema: The following tables are used for the questions below:

Table: Students

sid sname age city marks


1 Amit 17 Delhi 85

2 Priya 16 Mumbai 90

3 Rohan 18 Chennai 78

4 Sneha 17 Delhi 92

5 Vikram 16 Bangalore NULL

Table: Courses

cid cname fee duration

101 Python 5000 3

102 Java 6000 4

103 SQL 4000 2

Table: Enrollments

sid cid enrollment_date

1 101 2023-01-10

2 101 2023-02-15

3 102 2023-03-20

4 103 2023-04-05

5 101 2023-05-12

Questions 1–50: Write the MySQL query and expected output for each.

1. Retrieve all students from Delhi.


Query: SELECT * FROM Students WHERE city = 'Delhi';
Output:

sid sname age city marks

1 Amit 17 Delhi 85

4 Sneha 17 Delhi 92

2. List all course names and their fees.


Query: SELECT cname, fee FROM Courses;
Output:

cname fee
Python 5000

Java 6000

SQL 4000

3. Find the total number of students.


Query: SELECT COUNT(*) AS total_students FROM Students;
Output:

total_students

4. Retrieve students with marks greater than 80.


Query: SELECT sname, marks FROM Students WHERE marks > 80;
Output:

sname marks

Amit 85

Priya 90

Sneha 92

5. List unique cities from the Students table.


Query: SELECT DISTINCT city FROM Students;
Output:

city

Delhi

Mumbai

Chennai

Bangalore

6. Find students enrolled in the Python course.


Query: SELECT s.sname FROM Students s JOIN Enrollments e ON s.sid = e.sid JOIN Courses c
ON e.cid = c.cid WHERE c.cname = 'Python';
Output:

sname

Amit

Priya

Vikram
7. Retrieve the maximum marks from the Students table.
Query: SELECT MAX(marks) AS max_marks FROM Students;
Output:

max_marks

92

8. List students sorted by marks in descending order.


Query: SELECT sname, marks FROM Students ORDER BY marks DESC;
Output:

sname marks

Sneha 92

Priya 90

Amit 85

Rohan 78

Vikram NULL

9. Find the average fee of all courses.


Query: SELECT AVG(fee) AS avg_fee FROM Courses;
Output:

avg_fee

5000

10. List students who have not enrolled in any course.


Query: SELECT s.sname FROM Students s LEFT JOIN Enrollments e ON s.sid = e.sid WHERE
e.sid IS NULL;
Output:

sname

(None)

11. Retrieve the total number of enrollments.


Query: SELECT COUNT(*) AS total_enrollments FROM Enrollments;
Output:

total_enrollments

12. List courses with a duration of 3 months or less.


Query: SELECT cname, duration FROM Courses WHERE duration <= 3;
Output:
cname duration

Python 3

SQL 2

13. Find students aged 17 or older.


Query: SELECT sname, age FROM Students WHERE age >= 17;
Output:

sname age

Amit 17

Rohan 18

Sneha 17

14. Retrieve the minimum fee of all courses.


Query: SELECT MIN(fee) AS min_fee FROM Courses;
Output:

min_fee

4000

15. List students and their enrolled course names.


Query: SELECT s.sname, c.cname FROM Students s JOIN Enrollments e ON s.sid = e.sid JOIN
Courses c ON e.cid = c.cid;
Output:

sname cname

Amit Python

Priya Python

Rohan Java

Sneha SQL

Vikram Python

16. Find the total fee for all courses.


Query: SELECT SUM(fee) AS total_fee FROM Courses;
Output:

total_fee

15000
17. List students with NULL marks.
Query: SELECT sname FROM Students WHERE marks IS NULL;
Output:

sname

Vikram

18. Retrieve courses with fees between 4000 and 6000.


Query: SELECT cname, fee FROM Courses WHERE fee BETWEEN 4000 AND 6000;
Output:

cname fee

Python 5000

Java 6000

SQL 4000

19. Find the number of students per city.


Query: SELECT city, COUNT(*) AS student_count FROM Students GROUP BY city;
Output:

city student_count

Delhi 2

Mumbai 1

Chennai 1

Bangalore 1

20. List students enrolled after January 2023.


Query: SELECT s.sname, e.enrollment_date FROM Students s JOIN Enrollments e ON s.sid =
e.sid WHERE e.enrollment_date > '2023-01-31';
Output:

sname enrollment_date

Priya 2023-02-15

Rohan 2023-03-20

Sneha 2023-04-05

Vikram 2023-05-12

21. Find the course with the highest fee.


Query: SELECT cname, fee FROM Courses WHERE fee = (SELECT MAX(fee) FROM Courses);
Output:
cname fee

Java 6000

22. List students who enrolled in SQL or Python.


Query: SELECT s.sname, c.cname FROM Students s JOIN Enrollments e ON s.sid = e.sid JOIN
Courses c ON e.cid = c.cid WHERE c.cname IN ('SQL', 'Python');
Output:

sname cname

Amit Python

Priya Python

Sneha SQL

Vikram Python

23. Retrieve the average marks of students from Delhi.


Query: SELECT AVG(marks) AS avg_marks FROM Students WHERE city = 'Delhi';
Output:

avg_marks

88.5

24. List courses with no enrollments.


Query: SELECT c.cname FROM Courses c LEFT JOIN Enrollments e ON c.cid = e.cid WHERE
e.cid IS NULL;
Output:

cname

(None)

25. Find students with names starting with 'A'.


Query: SELECT sname FROM Students WHERE sname LIKE 'A%';
Output:

sname

Amit

26. Retrieve the number of enrollments per course.


Query: SELECT c.cname, COUNT(e.sid) AS enrollment_count FROM Courses c LEFT JOIN
Enrollments e ON c.cid = e.cid GROUP BY c.cname;
Output:

cname enrollment_count
Python 3

Java 1

SQL 1

27. List students with marks above the average marks.


Query: SELECT sname, marks FROM Students WHERE marks > (SELECT AVG(marks) FROM
Students);
Output:

sname marks

Priya 90

Sneha 92

28. Find the total duration of all courses.


Query: SELECT SUM(duration) AS total_duration FROM Courses;
Output:

total_duration

29. Retrieve students enrolled in more than one course.


Query: SELECT s.sname FROM Students s JOIN Enrollments e ON s.sid = e.sid GROUP BY
s.sname HAVING COUNT(e.cid) > 1;
Output:

sname

(None)

30. List courses sorted by fee in ascending order.


Query: SELECT cname, fee FROM Courses ORDER BY fee ASC;
Output:

cname fee

SQL 4000

Python 5000

Java 6000

31. Find students from Mumbai or Chennai.


Query: SELECT sname, city FROM Students WHERE city IN ('Mumbai', 'Chennai');
Output:

sname city
Priya Mumbai

Rohan Chennai

32. Retrieve the enrollment dates for Python course students.


Query: SELECT s.sname, e.enrollment_date FROM Students s JOIN Enrollments e ON s.sid =
e.sid JOIN Courses c ON e.cid = c.cid WHERE c.cname = 'Python';
Output:

sname enrollment_date

Amit 2023-01-10

Priya 2023-02-15

Vikram 2023-05-12

33. Find the number of students with marks above 85.


Query: SELECT COUNT(*) AS high_marks FROM Students WHERE marks > 85;
Output:

high_marks

34. List students and their course fees.


Query: SELECT s.sname, c.fee FROM Students s JOIN Enrollments e ON s.sid = e.sid JOIN
Courses c ON e.cid = c.cid;
Output:

sname fee

Amit 5000

Priya 5000

Rohan 6000

Sneha 4000

Vikram 5000

35. Retrieve the minimum marks from the Students table.


Query: SELECT MIN(marks) AS min_marks FROM Students;
Output:

min_marks

78

36. Find students enrolled before March 2023.


Query: SELECT s.sname, e.enrollment_date FROM Students s JOIN Enrollments e ON s.sid =
e.sid WHERE e.enrollment_date < '2023-03-01';
Output:

sname enrollment_date

Amit 2023-01-10

Priya 2023-02-15

37. List courses with a fee greater than 5000.


Query: SELECT cname, fee FROM Courses WHERE fee > 5000;
Output:

cname fee

Java 6000

38. Find the total number of students aged 16.


Query: SELECT COUNT(*) AS age_16 FROM Students WHERE age = 16;
Output:

age_16

39. Retrieve students with names ending with 'a'.


Query: SELECT sname FROM Students WHERE sname LIKE '%a';
Output:

sname

Priya

Sneha

40. List enrollments sorted by enrollment date.


Query: SELECT s.sname, e.enrollment_date FROM Students s JOIN Enrollments e ON s.sid =
e.sid ORDER BY e.enrollment_date;
Output:

sname enrollment_date

Amit 2023-01-10

Priya 2023-02-15

Rohan 2023-03-20

Sneha 2023-04-05

Vikram 2023-05-12
41. Find the average duration of all courses.
Query: SELECT AVG(duration) AS avg_duration FROM Courses;
Output:

avg_duration

42. List students who enrolled in Java.


Query: SELECT s.sname FROM Students s JOIN Enrollments e ON s.sid = e.sid JOIN Courses c
ON e.cid = c.cid WHERE c.cname = 'Java';
Output:

sname

Rohan

43. Retrieve the total fee paid by students from Delhi.


Query: SELECT SUM(c.fee) AS total_fee FROM Students s JOIN Enrollments e ON s.sid = e.sid
JOIN Courses c ON e.cid = c.cid WHERE s.city = 'Delhi';
Output:

total_fee

9000

44. Find the number of courses with a duration of 2 months.


Query: SELECT COUNT(*) AS short_courses FROM Courses WHERE duration = 2;
Output:

short_courses

45. List students with marks less than 80 or NULL.


Query: SELECT sname, marks FROM Students WHERE marks < 80 OR marks IS NULL;
Output:

sname marks

Rohan 78

Vikram NULL

46. Retrieve the course with the shortest duration.


Query: SELECT cname, duration FROM Courses WHERE duration = (SELECT MIN(duration)
FROM Courses);
Output:

cname duration
SQL 2

47. Find students and their enrollment dates for courses costing 5000 or more.
Query: SELECT s.sname, e.enrollment_date FROM Students s JOIN Enrollments e ON s.sid =
e.sid JOIN Courses c ON e.cid = c.cid WHERE c.fee >= 5000;
Output:

sname enrollment_date

Amit 2023-01-10

Priya 2023-02-15

Rohan 2023-03-20

Vikram 2023-05-12

48. List cities with more than one student.


Query: SELECT city, COUNT(*) AS student_count FROM Students GROUP BY city HAVING
COUNT(*) > 1;
Output:

city student_count

Delhi 2

49. Retrieve the names of students not enrolled in Java.


Query: SELECT s.sname FROM Students s JOIN Enrollments e ON s.sid = e.sid JOIN Courses c
ON e.cid = c.cid WHERE c.cname != 'Java';
Output:

sname

Amit

Priya

Sneha

Vikram

50. Find the total number of enrollments in courses with a fee of 5000.
Query: SELECT COUNT(*) AS enroll_count FROM Enrollments e JOIN Courses c ON e.cid =
c.cid WHERE c.fee = 5000;
Output:

enroll_count

You might also like