DataBase Assignments
DataBase Assignments
Q #1) What are Inner JOINS used in SQL? Explain with Syntax
Emp_ld, Last_Name, First_Name, Job Role (from the first table: Emp_details)
Emp_ld, Last_Name, First_Name, Joining_Date (from the second table:
Emp_details2)
They both have a common column, Emp_ld. We can use an inner join to combine
information about employees (including their job role) with their joining dates. Here's
the query:
select
emp_details.emp_id,emp_details.Last_name,Emp_details.First_name,Job_role,j
oining_date from emp_details inner join emp_details2 on
emp_details.Last_name=emp_details2.last_name AND
emp_details.first_name=emp_details2.First_name;
+--------+-----------+------------+----------------+--------------+-----------------+----+
| emp_id | Last_name |First_name | Job_role | joining_date |
+--------+-----------+------------+----------------+--------------+-----------------+-----+
| E0011 | Varma | Akhil | Administration | 2016-04-18 |
| E0012 | Samson | Nikita | Asst.Manager | 2016-04-19 |
| E0013 | Jordon | Nil | incharge | 2016-05-01 |
+--------+-----------+------------+----------------+--------------+--------------------+
Q #2) What are Left JOINS used in SQL? Explain with Syntax
A left join is a type of join used in SQL to combine data from two or more tables based
on a related column. In a left join, all rows from the left table are included in the result
set, even if there is no matching row in the right table. For rows in the right table that
don't have a match in the left table, the corresponding columns in the right table will be
filled with null values.
SELECT *
FROM left_table
LEFT JOIN right_table
ON left_table.column_name = right_table.column_name;
In this syntax:
SELECT * selects all columns from both tables. You can also specify the columns you
want to select instead of using *.
left_table is the table that will provide all the rows in the result set.
right_table is the table that will be joined with the left table.
ON left_table.column_name = right_table.column_name is the join condition that
specifies how the tables will be joined. The columns specified in the ON clause must
have compatible data types.
They both have a common column, Emp_ld. We can use a left join to combine
information about employees (including their job role) with their joining dates. Here's
the query:
select
emp_details.emp_id,emp_details.Last_name,Emp_details.First_name,Job_role,j
oining_date from emp_details left join emp_details2 on
emp_details.Last_name=emp_details2.last_name AND
emp_details.first_name=emp_details2.First_name;
+--------+-----------+------------+----------------+--------------+------------------+
| emp_id | Last_name | First_name | Job_role | joining_date |
+--------+-----------+------------+----------------+--------------+------------------+
| E0011 | Varma | Akhil | Administration | 2016-04-18 |
| E0012 | Samson | Nikita | Asst.Manager | 2016-04-19 |
| E0013 | Jordon | Nil | incharge | 2016-05-01 |
| E0014 | Smith | Joe | Technician | NULL |
+--------+-----------+------------+----------------+--------------+------------------+
Q #3) What are Right JOINS used in SQL? Explain with Syntax
A right join in SQL is a type of join used to combine data from two tables based on a
matching column. Unlike an inner join that returns only matching rows from both
tables, a right join prioritizes including all rows from the right table. It also includes
matching rows from the left table, but if there's no match in the left table, the
corresponding columns will be filled with NULL values.
SELECT *
FROM table1
RIGHT JOIN table2 ON table1.column1 = table2.column2;
SELECT *: This selects all columns from both tables. You can also specify individual
columns instead of *.
FROM table1: This specifies the first table involved in the join.
RIGHT JOIN table2: This keyword indicates a right join operation is being performed
on the second table (table2).
ON table1.column1 = table2.column2: This clause defines the join condition. Rows
from both tables are matched based on the equality
of column1 in table1 and column2 in table2.
Key points about right joins:
All rows from the right table are included in the result, even if there's no match in the
left table.
Matching rows from the left table are also included.
Unmatched rows from the left table will have NULL values in the corresponding
columns.
Right joins are useful when you want to ensure all data from the right table is included
in the result, even if there's no corresponding data in the left table. This can be helpful
for analyzing data from a specific table and identifying missing information in the other
table.
select
emp_details.emp_id,emp_details.Last_name,Emp_details.First_name,Job_role,j
oining_date from emp_details right join emp_details2 on
emp_details.Last_name=emp_details2.last_name AND
emp_details.first_name=emp_details2.First_name;
+--------+-----------+------------+----------------+--------------+----------------+
| emp_id | Last_name | First_name | Job_role | joining_date |
+--------+-----------+------------+----------------+--------------+----------------+
| E0011 | Varma | Akhil | Administration | 2016-04-18 |
| E0012 | Samson | Nikita | Asst.Manager | 2016-04-19 |
| E0013 | Jordon | Nil | incharge | 2016-05-01 |
| NULL | NULL | NULL | NULL | 2016-03-01 |
+--------+-----------+------------+----------------+--------------+-----------------+
Q #4) What are Full JOINS used in SQL? Explain with Syntax
A full join in SQL is a join operation that combines all rows from both participating
tables. It includes matching rows from both tables (like an inner join) and unmatched
rows from either table (filling in with NULL values for the missing columns).
Here's how a full join works:
It retrieves all rows from the left table and all rows from the right table.
For matching rows (based on the join condition), it includes the corresponding values
from both tables.
For rows that don't have a match in the other table, it fills the missing columns with
NULL values.
This essentially combines the results of a left outer join and a right outer join.
Syntax:
SELECT *
FROM table1
FULL OUTER JOIN table2 ON table1.column1 = table2.column2;
Analyzing complete datasets from two tables, even if there are missing matches.
Identifying missing information in one table compared to the other.
Getting a comprehensive view of data from both tables, regardless of matches.
Q #5)
Q1 Write an SQL query to fetch the current date-time from the system.
There are common functions you can use to fetch the current date-time from the
system:
Query:
SELECT CURRENT_TIMESTAMP;
+------------------------------------+
| CURRENT_TIMESTAMP |
+------------------------------------+
| 2024-03-27 19:38:04 |
+------------------------------------+
Q2. Find the Nth highest consultation fees from the PatientsCheckup table
with 4 and without using the TOP/LIMIT keywords.
solution to find the Nth highest consultation fee from the PatientsCheckup table
without using TOP/LIMIT keywords in MySQL:
1. Find the Nth Highest Consultation Fee: We can utilize a subquery to achieve this.
The subquery will find all distinct consultation fees ordered by descending order. We
can then use another query to find the Nth distinct fee from this result.
PatientsCheckup;
Here's a solution to find the Nth highest consultation fee from the PatientsCheckup
table without using TOP/LIMIT keywords in MySQL:
Find the Nth Highest Consultation Fee: We can utilize a subquery to achieve this. The
subquery will find all distinct consultation fees ordered by descending order. We can
then use another query to find the Nth distinct fee from this result.
Explanation:
The subquery:
ORDER BY ConsultationFees DESC sorts the fees in descending order, placing the
highest fee first.
LIMIT N-1, 1 limits the result to one record, starting from the (N-1)th position. This
ensures we retrieve the Nth highest distinct fee.
Example:
Assuming you want the 4th highest consultation fee (i.e., N = 4), the query would be:
+---------------------+
| 4thHighestFee |
+---------------------+
| 300 |
+----------------------+
MySQL doesn't support the TOP keyword for limiting results. However, you can
achieve the same functionality using the LIMIT clause. Here's the query to fetch the
top N records ordered by Consultation Fees in MySQL:
SELECT *
FROM PatientsCheckup
ORDER BY ConsultationFees DESC
LIMIT N;
Explanation:
1. SELECT *: This selects all columns from the PatientsCheckup table. You can
replace * with specific column names if needed.
2. ORDER BY ConsultationFees DESC: This clause sorts the results by
the ConsultationFees column in descending order, placing the highest fees first.
3. LIMIT N: This clause limits the number of returned rows to N. Replace N with the
desired number of top records you want to fetch.
Example:
SELECT *
FROM PatientsCheckup
LIMIT 5;
This query will return the details of the 5 consultations with the highest consultation
fees in the PatientsCheckup table.
+-------------+-----------+-----------+-------------------------+
+----------- +-----------+-----------+--------------------------+
| 5 | 143/67 | 78 | 700 |
| 4 | 160/81 | 61 | 550 |
| 2 | 142/76 | 78 | 400 |
| 1 | 121/80 | 67 | 300 |
| 3 | 151/75 | 55 | 300 |
+-------------+----------+------------+-------------------------+
Q4. Write a query to fetch even and odd rows from a table.
Fetching Even Rows:
SELECT *
FROM PatientsCheckup
WHERE MOD(column_name, 2) = 0;
Explanation:
SELECT *: This selects all columns from the PatientsCheckup table. You can replace
* with specific column names if needed.
SELECT *
FROM PatientsCheckup
Explanation:
SELECT *: This selects all columns from the PatientsCheckup table. You can replace
* with specific column names if needed.
SELECT *
FROM PatientsCheckup
Explanation:
This query is similar to the even rows query, but the condition uses <> 0 to select rows
where the remainder is not equal to 0, which represents odd-numbered positions
based on the column values.