You are on page 1of 34

(A) Create a table with the following attributes :

Dept_No integer PRIMARY KEY


Dept_Name character(30) UNIQUE
Location character(30) NOT NULL

Solution : Creating table “DEPARTMENT_1”


Create a table with the following attributes :

Empid integer PRIMARY KEY


Empname character(30) NOT NULL
Empsal integer NOT NULL
Empdept character(30) NOT NULL
Dept_No integer FOREIGN KEY

Solution : Creating table “EMPLOYEE_1”


B. Insert a new record.

Solution : Inserting a new record in “DEPARTMENT_1” Table.


Inserting a new record in “EMPLOYEE_1” table.
C. Add an attribute “Hobby” character(30) to the table EMPLOYEE_1.

Solution : Adding attribute “Hobby” character.


D. Update salary of an employee to 35000 where Empid = 101.

Solution : Updating salary of the employee.


E. Show Empid, Empname of all employees.

Solution : Showing Empid, Empname of all employees


F. Show records of all employees where salary is greater than 25000.

Solution : Showing records of employees


G. Show records of all employees whose Empdept is “FINANCE” and Hobby is either
“READING” or “WRITING”.

Solution : Showing records of employees


H. select all employees whose salary ranges from 25000 to 50000.

Solution : Selecting all employees


I. Select the names of all employees whose names start from “A”.

Solution : Selecting all names


J. Select the average salary of an employee.

Solution : Selecting the average salary of an employee


K. Count the total number of records.

Solution : Counting total number of records


L. Calculate the total salary of employees.

Solution : Calculating the total salary of employees


M. Display the employee names in lower case.

Solution : Displaying the employee names in lower case


N. Display the employee names in capitals.

Solution : Displaying the employee names in capitals


O. Display the length of the Hobby field. Also give its alias name as “Length of the hobby
field”.

Solution : Displaying length of the hobby field and giving its alias name as “Length of the
hobby field”
P. Count the total number of employees in each department with their average salary.

Solution : Counting the total number of employees in each department with their average
salary
Q. Count the total number of employees in each department with total salary greater than
50000.

Solution : Counting the total number of employees in each department with total salary
greater than 50000
R. Create a table with the following attributes :

Dept_No integer PRIMARY KEY


Dept_Name character(30) UNIQUE
Location character(30) NOT NULL

Solution : Creating table “DEPARTMENT_1”


S. Explain the concept of FOREIGN KEY using the above tables.

Solution : In the context of Relational Databases, a FOREIGN KEY is a field (or


collection of fields) in one table that uniquely identifies a row of another table or the same
table. In simpler words, the FOREIGN KEY is defined in a second table, unique KEY in
the first table.
A FOREIGN KEY is a key used to link two tables together.

Look at the following two tables :

“EMPLOYEE_1” Table :

Empid Empname Empsal Empdept Hobby Dept_No


101 ANDREW 30000 FINANCE WRITING 10
102 ARNAB 45000 MARKETING READING 20
103 MATTHEW 50000 IT SINGING 30
105 ASTON 25000 FINANCE READING 10
106 RONALD 28000 IT WRITING 30
107 REYONNA 32000 FINANCE READING 10
108 ANNA 38000 MARKETING SINGING 20

“DEPARTMENT_1” Table :

Dept_No Dept_Name Location


10 FINANCE DELHI
20 MARKETING MUMBAI
30 IT BANGLORE
 Notice that the “Dept_No” column in the “DEPARTMENT_1” Table points to the
“Dept_No” column in the “EMPLOYEE_1” Table.
 The “Dept_No” column in the “EMPLOYEE_1” Table is the PRIMARY KEY in
the “DEPARTMENT_1” Table.
 The “Dept_No” column in the “DEPARTMENT_1” Table is a FOREIGN KEY in
the “DEPARTMENT_1” Table.
 The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.
 The FOREIGN KEY constraint also prevents invalid data from being inserted into
the foreign key column, because it has to be one of the values contained in the table
it points to.

Table “DEPARTMENT_1”
Inserting values in table “DEPARTMENT_1”

Creation of “EMPLOYEE_1” table using concept of FOREIGN KEY from


“DEPARTMENT_1” table
T. How many JOINS are there ? Explain each with an example.

Solution : There are different types of JOINS in SQL. They are :


(INNER) JOIN : Returns records that have matching values in both tables.
LEFT(OUTER) JOIN : Return all records from the left table, and the matched records
from the right table.
RIGHT(OUTER) JOIN : Returns all records from the right table , and the matched
records fron the left table.
FULL(OUTER) JOIN : Return all records when there is a match in either lift or right
table.

INNER JOIN

LEFT JOIN
RIGHT JOIN

FULL JOIN

SQL LEFT JOIN

The LEFT JOIN keyword returns all records from the left table(Table 1), and the matched
records from the right table(Table 2). The result is NULL from the right side, if there is no
match.

LEFT JOIN SYNTAX :


SELECT column_name(s)
FROM Table1
LEFT JOIN table2 ON table1.column_name=table2. Column_name;
EXAMPLE :-

Customer’s Database

“Order’s Table”

SELECT Customers.CustomerName,
Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;
NOTE : The LEFT JOIN keyword returns all records from the left table (Customers) ,
even if there are no matches in the right table (Orders).

SQL RIGHT JOIN

The RIGHT JOIN keyword returns all records from the right table (table2) , and the
matched records from the left table (table1).
The result is NULL from the left side, when there is no match.

RIGHT JOIN SYNTAX :


SELECT column_name(s)
FROM table1
RIGHT JOIN table2 ON table1.column_name = tab le2.column_name;

SQL FULL OUTER JOIN


The FULL OUTER JOIN keyword return all records when there is a match in either left
(table1) or right (table2) table records.
NOTE : FULL OUTER JOIN can potentially return very large result-sets !

FULL OUTER JOIN SYNTAX :


SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2 ON table1.column_name = table2.column_name;
Customers Table

Order Table

EXAMPLE :

The following SQL statement selects all customers , and all orders.
SELECT Customers.CustomerName , Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName ;
RESULT

NOTE : The FULL OUTER JOIN keyword returns all the rows from the left table (
Customers) , and all the rows from the right table (Orders) . If there are rows in
“Customers” that do not have matches in “Orders” , or if there are rows in “Orders” that
do not have matches in “Customers” , those rows will be listed as well.
U. Create VIEWS for query P and Q .

Solution : VIEW For Query P

VIEW FOR QUERY Q


V. Delete all the records and remove both the tables.

Solution : Deleting all the records from EMPLOYEE_1

Deleting all the records from DEPARTMENT_1


Removing table EMPLOYEE_1

Removing table DEPARTMENT_1

You might also like