Sie sind auf Seite 1von 28

A.

Create a table “Department” with the following :


DEPT_NO integer PRIMARY KEY
DEPT_NAME character UNIQUE
LOCATION character NOT NULL

Solution : Create table “Depertment “

B. Insert a new record.


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

C. 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”

D. Insert a new record.


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

E. Add an attribute “hobby” characters (30) to the table employee.


Solution : Adding attribute “Hobby” character.

F. Insert record in “hobby” characters (30) to the table employee.


Solution : Updating Hobby of the employee.

G. Update salary of an employee to 35000 whose empid = 101.


Solution : Updating salary of the employee.

H. Create Both table in SQL


Show Empid, Empname of all employees.

Solution : Showing Empid, Empname of all employees


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

Solution : Showing records of employees


K. Show records of all employees whose empdept is ‘Finance’ and hobby is either
‘reading’ or ‘writing’.
Solution : Showing records of employees

L. Select all employees whose salary ranges from 25000 to 50000.


M. Select the names of all employees whose names start from ‘A’.
N. Select the average salary of an employee.
O. Count the total number of records.
P. Calculate the total salary of employees.
Q. Display the employee names in lower case.
R. Display the employee names in capitals.
S. Display the length of the hobby field. Also give its alias name as “Length of the
hobby field”.
T. Count the total number of employees in each department with their average
salary.
U. Count the total number of employees in each department with total salary
greater than 80000 sort the result-set in descending order.
V. Count the total number of employees in each department with total salary
greater than 90000
W. Delete all the records and remove both the tables.
Question 1 - Explain the concept of foreign key using the above tables.
 A FOREIGN KEY is a key used to link two tables together.
 A FOREIGN KEY is a field (or collection of fields) in one table that refers
to the PRIMARY KEY in another table.
 The table containing the foreign key is called the child table, and the
table containing the candidate key is called the referenced or parent
table.
 Look at the following two tables:
TABLE: EMPLOYEE

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
104 SHUBHAM 60000 HR GAMING 40
105 ASTON 25000 FINANCE READING 10
106 RONALD 28000 IT GAMING 30
107 REYONNA 32000 FINANCE READING 10
108 ANNA 38000 MARKETING SINGING 20
109 PRIYA 60000 HR WRITING 40

TABLE: DEPARTMENT

DEPT_NO DEPT_NAME LOCATION


10 FINANCE DELHI
20 MARKETING MUMBAI
30 IT BANGLORE
40 HR DELHI

Notice that the "Dept_no" column in the "employee" table points to the "Dept_no"
column in the "Department" table.
The "Dept_no" column in the " Department" table is the PRIMARY KEY in the
"Department " table.
The "Dept_no" column in the "employee" table is a FOREIGN KEY in the
" employee" 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.

Question 2 - How many joins are there? Explain each with an example.

Answer – four type of joins in sql


1. (INNER) JOIN: Returns records that have matching values in both
tables.

INNER JOIN Syntax

SELECT column_name
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;
SQL INNER JOIN Example

SELECT Department.Dept_name, employee.Empid


FROM Department
INNER JOIN employee ON Department.Dept_no = Employee. Dept_no;
2. LEFT (OUTER) JOIN: Return all records from the left table, and the matched
records from the right table.

SQL LEFT JOIN Example


SELECT Employee.Empid, Department.Dept_name
FROM Employee
LEFT JOIN Orders ON Employee.Dept_no= Department. Dept_no
ORDER BY Employee.Empid;

3. RIGHT (OUTER) JOIN: Return all records from the right table, and the
matched records from the left table.
SQL RIGHT JOIN Example
SELECT Department.Dept_name, Employee.Name, Employee.Empsal
FROM Department
RIGHT JOIN Employee ON Orders.Dept_no= Employee.Dept_no
ORDER BY Department.Dept_name;

4. FULL (OUTER) JOIN: Return all records when there is a match in


either left or right table.

SQL RIGHT JOIN Example


SELECT employee.Empname, Department.Dept_name
FROM employee
FULL OUTER JOIN Orders ON employee.Dept_no= Department.Dept_no
ORDER BY employee.Empname;

Das könnte Ihnen auch gefallen