ASSIGNMENT-03
To study and execute the below SQL commands:
1. ASC
2. DESC
3. GROUP BY
4. ORDER BY
5. DISTINCT
6. COUNT
7. MAX
8. MIN
9. AVG
10. Character Recognition
Objective:
The objective of this assignment is to understand and execute SQL
commands such as ASC, DESC, GROUP BY, ORDER BY,
DISTINCT, COUNT, MAX, MIN, AVG, and Character Recognition
using a students table.
Step 1: Creating the Students Table
Before executing the commands, we will create a table named students with
relevant columns.
SQL Query to Create the Table
Step 2: Executing SQL Commands
1. ASC (Ascending Order)
Sorting students based on marks in ascending order.
2. DESC (Descending Order)
Sorting students based on marks in descending order.
3. GROUP BY
Grouping students by city and calculating the average marks in each city.
4. ORDER BY
Sorting students by grade and then by marks in descending order.
5. DISTINCT
Retrieving distinct cities from the table.
6. COUNT
Counting the number of students in each grade.
7. MAX (Maximum Value)
Finding the highest marks obtained by any student.
8. MIN (Minimum Value)
Finding the lowest marks obtained by any student.
9. AVG (Average)
Calculating the average marks of all students.
10. Character Recognition
To proceed with these commands, we first have to add data matching the
commands,
The commands are as follows:
Explanation for the above outputs:
1. LIKE 'A' (Incorrect)
● This query attempts to find names that exactly match 'A'.
● Issue: The LIKE operator is used for pattern matching, and since 'A' is a single
character without wildcards, it will not return any results unless there is a name that is
just 'A'.
2. LIKE 'A%' (Names Starting with 'A')
● This query selects students whose names start with 'A'.
3. LIKE '_A%' (Second Letter is 'A')
● The underscore _ represents a single character.
● This query returns names where 'A' is the second character.
4. LIKE '__A%' (Third Letter is 'A')
● This query looks for names where 'A' appears as the third character.
5. LIKE '_A%R_' (Second Letter is 'A', Ends in 'R', and has at Least Four Letters)
● _A%R_ means:
● The second letter must be 'A'.
● There can be any characters in between.
● The last letter must be at least one more character after 'R'.
6. LIKE ; (Incorrect Syntax)
● This query is incorrect because the LIKE operator must be followed by a pattern
inside single or double quotes.