Unit 3
Unit 3
Basic SQL
SQL : Structured query language or Database language to perform operations on existing database to create DB.
Outer query can apply same condition on the result of the inner query.
Types;
Independent nested queries ,Co-related nested queries, Tables for understanding nested queries:
STUDENT:
C_ID C_Name
C1 SOM
C2 EM
C3 ED
STUDENT_COURSE:
S_ID C_ID
s1 c1
s2 c3
s3 c2
The functions which are grouped values of multiple rows to form a single value result.
Avg():Avg(salary) = sum(salary)/count(salary)
Min():Min(salary):
Max():Max(salary):
Create SQL queries to select the following information from the employee table:
Emp(id,name,dept,designation,sal,gender)
SQL editor:
create table
emp(id int primary key, name varchar(50), dept varchar(50),designation varchar(50),sal int,gender varchar(10));
insert into
emp(id,name,dept,designation,sal,gender)values(101,'ravi','civil','professor',50000,'m'),(102,'raju','cse','assistant
professor',30000,'m'),(103,'rani','eee','professor',80000,'f'),(104,'rama','cse','technician',20000,'f'),(105,'rekha','civil','
technician',15000,'f'),(106,'srinu','cse','data operator',10000,'m');
d name dept designation sal gender
assistant
102 raju cse 30000 m
professor
Output
dept employee_count
civil 2
cse 3
eee 1
Output
dept max_salary
civil 50000
cse 30000
eee 80000
select dept from emp where gender='f' group by dept order by count(*) desc limit 1;
Output
dept
eee
Output
dept total_slary
civil 65000
cse 60000
eee 80000
Output
dept name
civil ravi
eee rani
JOINS:
Joins in SQL are used to combine rows from two or more tables based on a related column between them. Here’s a
breakdown of the different types of joins:
An **inner join** returns only the rows that have matching values in both tables. It excludes rows where there is no
match.
**Syntax:**
```sql
SELECT columns
FROM table1
```
**Example:**
To find the names of employees along with their department names, we can use an inner join:
```sql
FROM Employees
```
An **outer join** returns all rows from one or both of the tables, even if there is no match. There are three types of
outer joins:
- **Left Outer Join (Left Join):** Returns all rows from the left table and the matched rows from the right table. If
there is no match, NULL values are returned for columns from the right table.
**Syntax:**
```sql
SELECT columns
FROM table1
```
**Example:**
```sql
FROM Employees
```
This will return all employees, even if they don't belong to any department.
- **Right Outer Join (Right Join):** Returns all rows from the right table and the matched rows from the left table. If
there is no match, NULL values are returned for columns from the left table.
**Syntax:**
```sql
SELECT columns
FROM table1
```
**Example:**
```sql
FROM Employees
```
This will return all departments, even if no employees are assigned to them.
- **Full Outer Join (Full Join):** Returns all rows when there is a match in either table. If there is no match, it returns
NULLs for missing matches from either side.
**Syntax:**
```sql
SELECT columns
FROM table1
```
**Example:**
```sql
```
This will return all employees and all departments, matching when possible and filling with NULLs when not.
A **self join** is a join in which a table is joined with itself. It is useful for comparing rows within the same table.
**Syntax:**
```sql
```
**Example:**
Suppose we have a `Employees` table with `EmployeeID`, `Name`, and `ManagerID`, where `ManagerID` references
`EmployeeID` in the same table. To find the names of employees along with their manager names, we can use a self
join:
```sql
FROM Employees e1
```
### Summary
- **Left/Right Outer Join:** Returns all rows from the left/right table, and matching rows from the other table.
- **Full Outer Join:** Returns all rows from both tables, with matching where possible.
- **Self Join:** Joins a table to itself to compare rows within the same table.
Each of these joins is useful in different scenarios depending on the data relationships you want to analyze or
retrieve.