SQL Lab Manual 3
SQL Lab Manual 3
Objective:
The objective of this assignment is to enhance understanding of advanced SQL concepts such as
grouping and filtering data using GROUP BY, HAVING, and LIMIT clauses, the use of aliasing,
understanding the order of execution of SQL queries, referential triggered actions, and advanced
pattern matching and comparison operators.
Clause:
GROUP BY
The GROUP BY Statement in SQL is used to arrange identical data into groups with the help of
some functions. i.e. if a particular column has the same values in different rows then it will
arrange these rows in a group.
Features
● GROUP BY clause is used with the SELECT statement.
● In the query, the GROUP BY clause is placed after the WHERE clause.
● In the query, the GROUP BY clause is placed before the ORDER BY clause if used.
● In the query, the Group BY clause is placed before the Having clause.
● Place condition in the having clause.
SYNTAX:
SELECT column1, aggregate_function_name(column2)
FROM table_name
WHERE condition
GROUP BY column1, column2
ORDER BY column1, column2;
HAVING
The HAVING clause was introduced in SQL to allow the filtering of query results based on
aggregate functions and groupings, which cannot be achieved using the WHERE clause that is
used to filter individual rows.
In simpler terms, the HAVING clause is used to apply a filter on the result of GROUP BY based
on the specified condition. The conditions are Boolean type i.e. use of logical operators (AND,
OR). This clause was included in SQL as the WHERE keyword failed when we use it with
aggregate expressions. Having is a very generally used clause in SQL. Similar to WHERE it
helps to apply conditions, but HAVING works with groups. If you wish to filter a group, the
HAVING clause comes into action.
Some important points:.
● The expression in the syntax can only have constants.
● In the query, ORDER BY is to be placed after the HAVING clause, if any.
● HAVING Clause is implemented in column operation.
● Having clause is generally used after GROUP BY.
SYNTAX:
SELECT col_1, aggregate_function_name(col_2)
FROM tablename
WHERE condition
GROUP BY col_1, col_2
HAVING Condition
ORDER BY col_1, col_2;
HAVING vs WHERE
HAVING WHERE
➔ The HAVING clause is used to filter ➔ The WHERE clause is used to filter rows
groups of rows after the grouping has before grouping occurs.
been done.
➔ HAVING is often used with aggregate ➔ WHERE cannot be used with aggregate
functions (like COUNT(), SUM(), functions; it only filters individual rows
AVG()). based on non-aggregated values.
➔ HAVING is executed after the GROUP ➔ WHERE is executed before the GROUP
BY clause. BY clause.
LIMIT
The LIMIT clause in SQL allows users to control the amount of data retrieved and displayed in
the result set.
It is useful when only a subset of records is needed for analysis or display purposes in large
databases with thousands of records.
SYNTAX:
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
Pagination Using MySQL LIMIT, OFFSET
Pagination allows users to view a subset of records at a time, making large datasets easier to
navigate and understand. This is especially useful in database-driven applications where large
amounts of data need to be displayed in a user-friendly way.
MySQL offers an efficient method for pagination through the use of the `LIMIT` and `OFFSET`
clauses.
Pagination is essential for breaking down large datasets into smaller, more manageable sections,
commonly used in web applications. In MySQL, pagination can be implemented using the
`LIMIT` and `OFFSET` clauses, allowing for precise control over which records are returned.
The basic syntax for using LIMIT and OFFSET in MySQL for pagination is as follows:
SYNTAX:
SELECT * FROM your_table
LIMIT number_of_records
OFFSET offset_value;
Key Points:
● LIMIT: Specifies the number of records to retrieve.
● OFFSET: Specifies the starting point for records retrieval.
NOTE :
The LIMIT clause is commonly supported by MySQL, PostgreSQL, and other database
management systems that implement the LIMIT keyword. However, not all databases use the
LIMIT clause. For example, in SQL Server, the equivalent functionality is achieved using the
TOP clause, while in Oracle, you would use the ROWNUM or FETCH FIRST syntax.
SYNTAX:
SELECT column1, aggregate_function(column2)
FROM table
GROUP BY column1
HAVING aggregate_function(column2) > value
LIMIT number
OFFSET offset_value;
Order of Execution of SQL Queries
The order in which different parts of a SQL query are logically processed.
FROM: The first step is to determine the data source (tables or views) from which to retrieve data. If there are any
JOIN operations, they are processed at this stage as well.
WHERE: The WHERE clause is applied next to filter the rows retrieved by the FROM clause. Only rows that
satisfy the WHERE condition are passed to the next step.
GROUP BY: After filtering, the rows are grouped based on one or more columns. The GROUP BY clause creates
groups of rows that have the same values in specified columns.
HAVING: The HAVING clause is applied to groups created by the GROUP BY clause. It filters the groups, keeping
only those that meet the specified condition.
SELECT: Now, the SELECT clause is processed. It determines which columns (or expressions) to include in the
final result set. If DISTINCT is used, duplicate rows are removed here.
ORDER BY: The ORDER BY clause sorts the result set based on one or more columns or expressions. This step is
optional, and the result can be sorted in ascending or descending order.
LIMIT: Finally, the LIMIT clause restricts the number of rows returned by the query, based on the specified
number. This step is also optional.
Aliasing
In MySQL, aliases are used to make the content of your database more readable. Aliases are used
to give columns or tables a temporary name.
● Aliases are often used to make column names more readable.
● This makes the result of the query more clear.
● There are two aliases in MySQL, which are column alias and table alias.
● In column alias, you give a name to a column using the ‘AS’ keyword.
● Table aliases are mostly used in performing complex joins where you need to write
table names many times, by using aliases this can be minimized.
● The renaming is just a temporary change and the table name does not change in the
original database.
SYNTAX
SELECT column_name AS alias_name
FROM table_name;
Referential Triggered Action
● SET NULL: Sets the foreign key value to NULL when the parent record is deleted.
● SET DEFAULT: Sets the foreign key to a default value when the parent record is deleted.
● SET CASCADE: Automatically updates or deletes the foreign key when the parent
record is updated or deleted.
SYNTAX
CREATE TABLE child_table (
child_column INT,
parent_column INT,
FOREIGN KEY (parent_column) REFERENCES parent_table(parent_column)
ON DELETE SET NULL
ON UPDATE CASCADE
);
Assignment 3 Questions
Objective:
a. Create the Students table and insert the provided data.
b. Execute all the queries and verify the output.
1. Write the SQL statement to create the Students table with the following schema:
a. studentid INT,
b. studentname NVARCHAR(255),
c. subject NVARCHAR(255),
d. marks INT,
e. testid INT,
f. testdate DATE
2. Insert the provided values into the Students table:
(2, 'Max Ruin', 'Subject1', 63, 1, '2022-01-02'),
(3, 'Arnold', 'Subject1', 95, 1, '2022-01-02'),
(4, 'Krish Star', 'Subject1', 61, 1, '2022-01-02'),
(5, 'John Mike', 'Subject1', 91, 1, '2022-01-02'),
(4, 'Krish Star', 'Subject2', 71, 1, '2022-01-02'),
(3, 'Arnold', 'Subject2', 32, 1, '2022-01-02'),
(5, 'John Mike', 'Subject2', 61, 2, '2022-11-02'),
(1, 'John Deo', 'Subject2', 60, 1, '2022-01-02'),
(2, 'Max Ruin', 'Subject2', 84, 1, '2022-01-02'),
(2, 'Max Ruin', 'Subject3', 29, 3, '2022-01-03'),
(5, 'John Mike', 'Subject3', 98, 2, '2022-11-02')
3. Write a query to group students by subject and show the total marks for each subject.
4. Write a query to display the average marks per testid for each subject.
5. Write a query to count the number of students in each subject.
6. Write a query to find subjects that have a total mark greater than 200.
7. Find the subjects where the average marks are more than 60.
8. Write a query to find all the subjects where more than 3 tests have been taken.
9.
10. Write a query to list all students and their marks in ascending order of marks.
11. Write a query to display the students in descending order of marks within each subject.
12. List all records sorted by testdate and marks in ascending order.
13. Write a query to display student name and total marks, aliasing total marks as TotalScore.
14. Write a query to display the subject and the average marks, aliasing the average marks as
AvgScore.
15. Write a query to return the top 5 students with the highest marks.
16. Write a query to skip the first 3 rows and display the next 5 rows ordered by marks.
17. Retrieve the records of students who have taken more than one test, sorted by studentid
but limit the output to only the first 4 rows.
18. Write a query to list studentname and total marks for students who scored more than 200
marks across all tests. Sort the result by total marks in descending order.
19. Write a query to display the number of tests taken by each student, where the number of
tests is greater than 1, ordered by studentname.
20. Write a query to find the highest and lowest marks in the table.
21. Write a query to find the average marks for students who have taken multiple tests.
22. Find the total number of records in the students table.
23. Write a query to get the total marks for each subject where the total marks are greater
than 150, and sort the result in descending order.
24. Find the students who have taken exactly two unique subjects and sort the results by
studentname.
25. Write a query to display studentname and the total marks of students who either have
total marks greater than 200 or have appeared in more than 2 tests and have scored
more than 50 marks in total.
Company Related Questions and Platform
Question 1 : LINK
We define an employee's total earnings to be their monthly salary * months worked, and the
maximum total earnings to be the maximum total earnings for any employee in the Employee
table. Write a query to find the maximum total earnings for all employees as well as the total
number of employees who have maximum total earnings. Then print these values as
space-separated integers.
Question 2 : LINK
Write a solution to report the movies with an odd-numbered ID and a description that is not
"boring". Return the result table ordered by rating in descending order.
Question 3 : LINK
Query the Western Longitude (LONG_W) for the largest Northern Latitude (LAT_N) in
STATION that is less than 137.2345 . Round your answer to 4 decimal places.
Question 4 : LINK
Query the Western Longitude (LONG_W)where the smallest Northern Latitude (LAT_N) in
STATION is greater than 38.7780. Round your answer to 4 decimal places.
Question 5 : LINK
Query the Manhattan Distance between points P1 and P2 and round it to a scale of 4 decimal
places.
Question 6 : LINK
Write a solution to calculate the number of unique subjects each teacher teaches in the university.
Return the result table in any order.
Question 7 : LINK
Consider P1(a, c) and P2(b,d) to be two points on a 2D plane where (a, b) are the respective
minimum and maximum values of Northern Latitude (LAT_N) and (c, d) are the respective
minimum and maximum values of Western Longitude (LONG_W) in STATION.
Query the Euclidean Distance between points P1 and P2 and format your answer to display 4
decimal digits
Question 8 : LINK
Write a solution to find all the classes that have at least five students. Return the result table in
any order.
Question 9 :
SQL Queries on Products and Sales Data of Bewkoof (Asked in Bewkoof company assessment)
Scenario: The following exercise is based on a simple e-commerce scenario where we manage
product information and sales records for an online clothing store named Bewkoof. We have two
tables:
You will work with SQL queries to retrieve specific information from these tables.
Table Definitions:
1. ProductsBewkoof table:
);
Insertion Values:
INSERT INTO ProductsBewkoof (product_id, product_name, unit_price)
VALUES
('A', 'T-Shirt', 1000),
('B', 'Short', 500);
Write an SQL query to calculate the total revenue generated for each product in the month
of January 2023.