0% found this document useful (0 votes)
5 views5 pages

Untitled Document (2)

The document outlines SQL commands for creating and managing database tables for departments and employees, including creating, altering, and deleting columns. It also includes various queries for selecting employee data based on specific criteria, as well as procedures and views for fetching employee details. Additionally, it describes the creation of a Company table and related operations, demonstrating the use of foreign keys and joins in SQL.

Uploaded by

tiwariddivya128
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Untitled Document (2)

The document outlines SQL commands for creating and managing database tables for departments and employees, including creating, altering, and deleting columns. It also includes various queries for selecting employee data based on specific criteria, as well as procedures and views for fetching employee details. Additionally, it describes the creation of a Company table and related operations, demonstrating the use of foreign keys and joins in SQL.

Uploaded by

tiwariddivya128
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Create department table:

CREATE TABLE department (


Dept_id INT PRIMARY KEY,
D_Name NVARCHAR(100) NOT NULL,
Contact_no INT UNIQUE
);

2. Create employee table (with foreign key):

CREATE TABLE employee (


Emp_id INT PRIMARY KEY,
Dept_id INT FOREIGN KEY REFERENCES department(Dept_id),
Emp_name NVARCHAR(100),
Designation NVARCHAR(100),
Salary MONEY
);

3. Add a new column (City) to the department table:

ALTER TABLE department ADD City NVARCHAR(50);

4. Change the data type of Salary to CHAR(10) in the employee table (Not recommended):

ALTER TABLE employee ALTER COLUMN Salary ALTER TYPE CHAR(10);

5. Delete the 'City' column from the department table:

ALTER TABLE department DROP COLUMN City;

6. Rename a column (D_NAME) in the department table to (Dept_NAME):

ALTER TABLE department RENAME COLUMN D_Name TO Dept_Name;


7. Insert values into the employee table:

INSERT INTO employee (Emp_id, Dept_id, Emp_name, Designation, Salary, contact_no, city)

VALUES (emp-1, dep-1, 'S Ahmad', 'Sales Mgr', 50000, 0110, New Delhi),
(emp-2, dep-2, 'Anand', 'Senior Mgr', 40000, 0111, New Delhi),
(emp-3, dep-3, 'Aruna', 'Accounts Mgr', 45000, 0112, New Delhi),
(emp-4, dep-3, 'Alpesh', 'Accountant', 35000, 0113, Banglore),
(emp-5, dep-1, 'Monica', 'Incharge', 25000, 0114, Noida),
(emp-6, dep-1, 'Harish', 'Sales Man', 15000, 0115, Banglore);

8. Select specific columns from the employee table:

SELECT Emp_id, Emp_name, Desig


FROM employee;

9. Select all details of employees with a salary greater than 30000:

SELECT *
FROM employee
WHERE Salary > 30000;

10. Select details of employees with a salary between 15000 and 30000:

SELECT *
FROM employee
WHERE Salary BETWEEN 15000 AND 30000;

11. Select all details of employees who live in 'Bangalore' or 'New Delhi' (assuming a City
column exists):

SELECT *
FROM employee
WHERE City IN ('Bangalore', 'New Delhi');

12. Select all details of employees who do not stay in 'Bangalore' and 'New Delhi'
(assuming a City column exists):

SELECT *
FROM employee
WHERE City NOT IN ('Bangalore', 'New Delhi');
13. Select details of employees whose name starts with the character 'A':

SELECT *
FROM employee
WHERE Emp_name LIKE 'A%';

14. Order employee details by salary in descending order:

SELECT *
FROM employee
ORDER BY Salary DESC;

15. Retrieve the average salary of employees per department:

SELECT department.Dept_name, AVG(employee.Salary) AS Average_Salary


FROM employee
INNER JOIN department ON employee.Dept_id = department.Dept_id
GROUP BY department.Dept_name;

16. Get department ID, salary, and average salary for departments with an average salary
greater than 30000:

SELECT d.Dept_id, e.Salary, AVG(e.Salary) AS Average_Salary


FROM employee e
INNER JOIN department d ON e.Dept_id = d.Dept_id
GROUP BY d.Dept_id, e.Salary
HAVING AVG(e.Salary) > 30000;

Step 1. Create Company Table

CREATE TABLE Company (


Emp_id INT PRIMARY KEY,
Name NVARCHAR(50) NOT NULL,
Age INT,
Address NVARCHAR(50),
Salary DECIMAL(8,2),
Join_date DATE
);
Step 2. Insert Data into Company Table

INSERT INTO Company (Emp_id, Name, Age, Address, Salary, Join_date)


VALUES (1, 'PAUL', 32, 'CALIFORNIA', 20000.00, '2001-07-13'),
(3, 'ALLEN', 23, ‘NORWAY', 20000.00),
(4, 'DAVID', 25, 'RICHMOND', 65000.00, '2010-10-25'),
(5, 'MARK', 27, 'TEXAS', 35000.00, '2015-11-02'),
(2, 'TEDDY', 25, 'LOS VEGAS', '2013-09-01');

Step 3. Create Dept Table

CREATE TABLE Dept (


Id INT PRIMARY KEY,
Dept NVARCHAR(20) NOT NULL,
Emp_id INT FOREIGN KEY REFERENCES Company(Emp_id)
);

Step 4. Insert Data into Dept Table

SQL
INSERT INTO Dept (Id, Dept, Emp_id)
VALUES (1, 'IT BILLING', 1),
(2, 'ENGINEERING', 2),
(3, ‘FINANCE', 41);

Query 1: Fetch Details for Employee with ID = 2 (Using JOIN)

SELECT c.Emp_id, c.Name, d.Dept, d.Id, c.Age, c.Salary


FROM Company c
INNER JOIN Dept d ON c.Emp_id = d.Emp_id
WHERE c.Emp_id = 2;

Step 18: Stored Procedure to Fetch Employee Details

CREATE PROCEDURE GetEmployeeDetails(IN emp_id INT)


AS
BEGIN
SELECT c.Emp_id, c.Name, d.Dept, d.Id, c.Age, c.Salary
FROM Company c
INNER JOIN Dept d ON c.Emp_id = d.Emp_id
WHERE c.Emp_id = emp_id;
END;

CALL GetEmployeeDetails(2);

Step 19: View to Fetch Employee Details

SQL
CREATE VIEW EmployeeDetails AS
SELECT c.Emp_id, c.Name, d.Dept, d.Id, c.Age, c.Salary
FROM Company c
INNER JOIN Dept d ON c.Emp_id = d.Emp_id;

SELECT * FROM EmployeeDetails;

You might also like