Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
45 views

SQL Lab Manual 4

Lab manual four

Uploaded by

koushal2023
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

SQL Lab Manual 4

Lab manual four

Uploaded by

koushal2023
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DBMS Lab Assignment 4

Objective:
The objective of this assignment is to understand the various types of SQL joins used to combine
records from two or more tables, and to explore the use of date and time functions as well as
conditional logic using CASE statements. You will learn to retrieve and manipulate data
effectively through practical examples and queries.

JOINS

Joins are used in SQL to combine rows from two or more tables based on a related column
between them. Different types of joins retrieve different combinations of matched and
unmatched data from the tables.

INNER JOIN
The `INNER JOIN` clause is one of the most frequently used joins in SQL. It combines rows
from two or more tables based on a related column between them. The `INNER JOIN` only
returns the rows where there is at least one match between the tables. If a row in the left table
does not have a corresponding match in the right table, it will be excluded from the result set.
This ensures that only the matched records are returned, making it a powerful tool for extracting
meaningful data from multiple tables.
SYNTAX
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

LEFT JOIN
In databases, data is often distributed across multiple tables, and JOIN statements allow us to
retrieve related data efficiently. The LEFT JOIN in MySQL, also known as a left outer join, is
one such powerful tool that merges tables based on a common column.

The LEFT JOIN returns all records from the left table and the matching records from the right
table. If no match exists in the right table, NULL values are returned for columns from the right
table. This join ensures that every record from the left table is included in the result, regardless of
whether a match is found.

SYNTAX
SELECT column1, column2, ...
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;

RIGHT JOIN

The RIGHT JOIN returns all records from the right table and the matching records from the left
table. If no match exists in the left table, NULL values are returned for the left table columns.
This join ensures that all rows from the right table are included in the result, whether or not they
have matching rows in the left table.

SYNTAX
SELECT column1, column2, ...
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

FULL JOIN

FULL JOIN retrieves all records where there is a match in either table. It combines the effects of
both LEFT JOIN and RIGHT JOIN. If no match is found, NULL values are returned for columns
where a match is missing.
SYNTAX
SELECT column1, column2, ...
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

NOTE: In MySQL, there is no direct support for the FULL JOIN . You can achieve the same
result by using a combination of LEFT JOIN and RIGHT JOIN with the UNION operator.
SYNTAX
SELECT column1, column2, ...
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name

UNION

SELECT column1, column2, ...


FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name

CASE Function

The CASE function is a conditional statement that evaluates a set of conditions and returns a
result when the first true condition is met. Similar to an if-then-else statement, once a condition
is satisfied, the function stops checking further conditions. If none of the conditions are met, the
function returns the value specified in the ELSE part. If no ELSE clause is provided, and no
conditions are met, it returns NULL.

The CASE function is versatile and can be used with SELECT, WHERE, ORDER BY, and other
clauses to introduce conditional logic within SQL queries.

Features of MySQL CASE Function:

● The function returns a value when the first condition is true.


● If no condition is met, the function returns the value specified in the ELSE part.
● If no ELSE part is provided and no condition is met, it returns NULL.
● The function can be used within SELECT, WHERE, and ORDER BY clauses.
● It is classified under Advanced Functions.
● The CASE function accepts two parameters: conditions and their respective results.

SYNTAX

SELECT column_name,
CASE
WHEN condition THEN result1
WHEN condition THEN result2
ELSE result3
END
FROM table_name;

Example

SELECT name,
CASE
WHEN marks >= 90 THEN 'A'
WHEN marks >= 80 THEN 'B'
ELSE 'C'
END AS grade
FROM students;
Date and Time Functions

In SQL, working with dates can be challenging for beginners because the format of the date in
the database table must align with the input data for successful insertion. In many cases,
`datetime` is used instead of just `date`, as it includes both the date and time, making it more
versatile for scenarios where time tracking is also necessary.

Here are common date and time functions with their uses:
NOW(): Returns the current date and time.
SELECT NOW();
CURDATE(): Returns the current date.
SELECT CURDATE();
CURTIME(): Returns the current time.
DATE(): Extracts the date part from a datetime expression.
SELECT DATE(CURRENT_TIMESTAMP);
DATE_ADD(): Adds a specified time interval to a date.
SELECT DATE_ADD(CURDATE(), INTERVAL value unit);
DATE_SUB(): Subtracts a specified time interval from a date.
SELECT DATE_SUB(CURDATE(), INTERVAL value unit);
DATEDIFF(): Returns the difference between two dates.
SELECT DATEDIFF(date1, date2);
YEAR(): Extracts the year from a date.
SELECT YEAR(CURDATE());
MONTH(): Extracts the month from a date.
SELECT MONTH(CURDATE());
DAY(): Extracts the day of the month from a date.
SELECT DAY(CURDATE());
Company Related Questions and Platform
Question 1: LINK
Write a query identifying the type of each record in the TRIANGLES table using its three side
lengths. Output one of the following statements for each record in the table:

Equilateral : It's a triangle with 3 sides of equal length.


Isosceles : It's a triangle with 2 sides of equal length.
Scalene : It's a triangle with 3 sides of differing lengths.
Not A Triangle: The given values of A, B, and C don't form a triangle.

Question 2 : LINK
Each node in the tree can be one of three types:
● "Leaf": if the node is a leaf node.
● "Root": if the node is the root of the tree.
● "Inner": If the node is neither a leaf node nor a root node.
Write a solution to report the type of each node in the tree. Return the result table in any order.

Question 3: LINK
Write a solution to find the IDs of the users who visited without making any transactions and the
number of times they made these types of visits. Return the result table sorted in any order.

Question 4 : LINK
Write an SQL query that reports the average experience years of all the employees for each
project, rounded to 2 digits. Return the result table in any order.

Question 5 : LINK
Write a solution that will, for each user, return the number of followers. Return the result table
ordered by user_id in ascending order.

Question 6 : LINK
Write a solution to report the customer ids from the Customer table that bought all the products
in the Product table. Return the result table in any order.

Question 7 : LINK
Report for every three line segments whether they can form a triangle. Return the result table in
any order.

Question 8 : LINK
Given the CITY and COUNTRY tables, query the names of all the continents
(COUNTRY.Continent) and their respective average city populations (CITY.Population) rounded
down to the nearest integer.
Note: CITY.CountryCode and COUNTRY.Code are matching key columns.
Question 9 :

Problem Statement:

Given the following Accounts table with columns account_id and income, write an SQL query to
calculate the number of bank accounts for each salary category. The salary categories are defined
as:

● "Low Salary": All salaries strictly less than $20,000.


● "Average Salary": All salaries in the inclusive range [$20,000, $50,000].
● "High Salary": All salaries strictly greater than $50,000.

The result table must contain all three categories, and if there are no accounts in a category,
return 0 for that category.

Create Accounts Table and insert the following data:


CREATE TABLE Accounts (
account_id INT,
income INT
);

INSERT INTO Accounts (account_id, income) VALUES


(3, 108939),
(2, 12747),
(8, 87709),
(6, 91796);

Write an SQL query to calculate the number of accounts in each salary category (Low
Salary, Average Salary, High Salary) using a CASE statement.

You might also like