You are on page 1of 13

Define sql.

Explain various datatypes in sql

explain various languages in sql

) Explain DDL commands in detail with an example. 5M b) Define join. Explain various types of join
with examples 10M

) Define view. Explain views in detail with example 5M b) Explain binary relational operations in
relational algebra with an example. 10M

Describe command constraints and explain insert , delete , rename commands with example

explain all the command statements in DDL with example

Explain any 5 data types in SQL 10m Create a table on student details and insert 5 records in it 5m

) Write SQL queries for the following given schema SALESPERSON(empid, Name, Start_year,
Dept_no) TRIP(empid, From_city, To_city, Departure_date, Return_date, Trip_id) EXPENSE(Trip_id,
Account_number, Amount) i) find the trips made by employee whose id=1001 from month of JAN to
JUNE 1025. 10m b) use create,insert and remane commands on the above table 5m

Examine the parts of a basic SQL query How can you obtain a set of tuples as the result of a query

write SQL queries for the following given schema SALESPERSON(empid, Name, Start_year, Dept_no)
TRIP(empid, From_city, To_city, Departure_date, Return_date, Trip_id) EXPENSE(Trip_id,
Account_number, Amount) i) find the trips made by employee whose id=1001 from month of JAN to
JUNE 1025. ii) find all the trip ids whose spent amount is in the range 30000 to 50000 per trip. iii)
find the total amount spent by an employee with id 1101 in the year 2015.

list SQL built in aggregate functions 5m Consider the following schema Employees(empid:
number ,empname: string ,deptid: number, managerid: number,job_id:number,salary:real,
hiredate: date) Departments(deptid:number, deptname:string, locationid: number) Jobs(jobid:
number, jobtitle: string, minsalary: real, maxsalary: real) Write SQL queries for the following i)find
the departments having maximum salary greater than 10000. ii)Compute the average salary of
each job type of each 10m

outline the importance of Union, Intersection, Minus Operstion.. 5m List different types of JOIN
operations, describe functions and procedures 10m

1) number, salary: real, hiredate: date)


Departments(deptid:number, deptname:string, locationid: number)
Jobs(jobid: number, jobtitle: string, minsalary: real, maxsalary: real)
Write SQL queries for the following
i) Write SQL queries to define tables for the given schemas with constraints.
Ans:) create table Employees(empid int ,empname: varchar ,deptid: int ,
managerid: int, job_id: int, salary: int, hiredate: date)
ii)Write SQL Queries to insert data in to employee, department, jobs.
Ans:) insert into employees values(10,’john’,20,15,33,10000,’12-02-2016’) etc.
iii) find the departments having maximum salary greater than 10000.
Ans:) select deptid from employees group by deptid having max(salary)>10000;
iv) Compute the average salary of each job type of each department.
Ans:) select job_id, avg(sal) from employees group by dept_id, job_id;
v) List all the employees names and the and experience in that company in weeks .
Ans:) select empname,datediff(systdate,hiredate)/7 from employees
his type of join is useful when we know the joining conditions.
Syntax:
SELECT Column_list
FROM Table1, Table 2
WHERE Join_Condition
Types of Join:
1.Natural Join
• A natural join returns all rows by matching values in comman columns having same name
and data types of columns and that column should be present in both tables.
• Natural join eliminates duplicate columns present in JOIN table. Therefore comman column
will be printed only once in resultant table.
Syntax:
SELECT Column_List
FROM Table1
NATURAL JOIN
Table 2
Employee Table

Eid EName Did

1 Mahesh 100

2 Suhas 200

3 Jayendra 300

Department Table

Did Dname

100 HR
Did Dname

200 TIS

SELECT E.Eid[Eid],
D.Did[Did]
D.Dname[Dname]
FROM Employee E
NATURAL JOIN
Department D

Eid Did DName

1 100 HR

2 200 TIS

3 300 HR

2.Cartesian Product/CROSS JOIN


• CROSS join occur due to WHERE condition is missing in query or some invalid operations
in where clause leads to undesired results or CROSS Join.
Syntax:
SELECT Column_List
FROM Table1
CROSS JOIN
Table 2
Employee Table

Eid EName Did

1 Mahesh 100

2 Suhas 200

3 Jayendra 300

Department Table

Did DName

100 HR

200 TIS

SELECT e.Eid[Eid],
e.Ename[Ename]
e.Did[Did]
d.Did[Did]
d.Dname[Dname]
FROM Employee e
CROSS JOIN
Department d
Eid EName Did Did Dname

1 Mahesh 100 100 HR

1 Mahesh 100 200 TIS

2 Suhas 200 100 HR

2 Suhas 200 200 TIS

3 Jayendra 100 100 HR

3 Jayendra 100 200 TIS

3.Self Join
•Any table can be joined by itself as long as aeachtable reference is given different name
using table alias.
Example:
Employee Table (E)

Eid Ename Mid

1 Mahesh 2

2 Suhas 3
Eid Ename Mid

3 Jayendra 3

Manager Table(M)

Eid Ename Mid

1 Mahesh 2

2 Suhas 3

3 Jayendra 3

SELECT E.Ename[Employee],
M.Ename[Manager]
FROM Employee E
CROSS
EmployeeM
ON E.Mid=M.Eid

Employee Manager

Mahesh Suhas

Suhas Jayendra
Employee Manager

Jayendra Jayendra

4.Inner Join
•Inner Join joins two table when there is atleast one match between two tables.
Syntax:
SELECT Column_List
FROM Table1
INNER JOIN
Table 2
ON (JOIN_Condition)
Employee Table

Eid EName Did

1 Mahesh 100

2 Suhas 200

3 Jayendra 300

Department Table

Did DName

50 DEV
Did DName

100 HR

200 TIS

SELECT E.Eid[Eid],
D.Did[Did]
D.Dname[Dname]
FROM Employee E
INNER JOIN
Department D
ON E.Did=D.DeptId

Eid Did Dname

1 100 HR

2 200 TIS

3 300 HR

5. Outer Join
•The join that can be used to see rows of tables that do not meet the join condition along with
rows that satisfies join condition is called as Outer Join.
a)Left Outer Join:
•A left outer join returns all the rows for which the join condition is true and returns all other
rows from the dominant table and displays the corresponding values from the subordinate
table as NULL.
Employee Table
Eid EName Did

1 Mahesh 100

2 Suhas 200

3 Jayendra 500

Department Table

Did DName

50 DEV

100 HR

200 TIS

SELECT E.Eid[Eid],
D.Did[Did]
D.Dname[Dname]
FROM Employee E
LEFT OUTER JOIN
Department D
ON E.Did=D.DeptId
Eid Did Dname

1 100 HR

2 200 TIS

3 500 NULL

a) Right Outer Join:


• A right outer join returns all the rows for which the join condition is true and returns all
other rows from the dominant table and displays the corresponding values from the
subordinate table as NULL.
Employee Table

Eid EName Did

1 Mahesh 100

2 Suhas 200

3 Jayendra 500

Department Table

Did DName

50 DEV
Did DName

100 HR

200 TIS

SELECT E.Eid[Eid],
D.Did[Did]
D.Dname[Dname]
FROM Employee E
RIGHT OUTER JOIN
Department D
ON E.Did=D.DeptId

Eid Did Dname

1 100 HR

2 200 TIS

NULL 50 DEV

c)Full Outer Join:


A full outer join returns all the rows for which the join condition is true and returns
i)All other rows from the right table and displays the corresponding values from the left table
as NULL.
ii)All other rows from the left table and displays the corresponding values from the right table
as NULL.
Employee Table
Eid EName Did

1 Mahesh 100

2 Suhas 200

3 Jayendra 500

Department Table

Did DName

50 DEV

100 HR

200 TIS

SELECT E.Eid[Eid],
E.Did[Did],
D.Did[Did],
D.Dname[Dname]
FROM Employee E
FULL OUTER JOIN
Department D
ON E.Did=D.DeptId
Eid Did Dname

1 100 HR

2 200 TIS

3 500 NULL

NULL 50 DEV

SQL join statements allow us to access information from two or more tables
at once. They also keep our database normalized. Normalization allows us
to keep data redundancy low so that we can decrease the amount of data
anomalies in our application when we delete or update a record.
A JOIN clause allows us to combine rows from two or more tables based on a
related column.

The type of join statement you use depends on your use case. There are four
different types of join operations:

 (INNER) JOIN: Returns dataset that have matching values in both


tables
 LEFT (OUTER) JOIN: Returns all records from the left table and
matched records from the right s
 RIGHT (OUTER) JOIN: Returns all records from the right table
and the matched records from the left
 FULL (OUTER) JOIN: Returns all records when there is a match
in either the left table or right table

You might also like