0% found this document useful (0 votes)
12 views9 pages

Document

The document contains SQL queries for creating and managing four tables: emp, dept, stud_master, and stud_detail, along with various operations such as inserting records, selecting data, and modifying table structures. It includes specific queries for retrieving employee and student information, as well as demonstrating joins and data manipulation techniques. Additionally, it provides explanations of SQL concepts like joins and self joins.

Uploaded by

likith9380
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)
12 views9 pages

Document

The document contains SQL queries for creating and managing four tables: emp, dept, stud_master, and stud_detail, along with various operations such as inserting records, selecting data, and modifying table structures. It includes specific queries for retrieving employee and student information, as well as demonstrating joins and data manipulation techniques. Additionally, it provides explanations of SQL concepts like joins and self joins.

Uploaded by

likith9380
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/ 9

CREATE TABLE emp ( EMPNO INT, ENAME VARCHAR(255),JOB VARCHAR(255),HIREDATE

DATE, SAL DECIMAL(10, 2), MODE VARCHAR(255),DEPTNO INT);

CREATE TABLE dept ( DEPTNO INT, DNAME VARCHAR(255), LOCATION VARCHAR(255));

CREATE TABLE stud_master ( REG_NO INT, S_NAME VARCHAR(255), BRANCH


VARCHAR(255));

CREATE TABLE stud_detail ( REG_NO INT, COURSE_CODE VARCHAR(255),


COURSE_NAME VARCHAR(255), MARKS INT, SEM INT);

I can provide you with the SQL queries in a document format. Since I can't directly create
and save a .doc file, I'll give you the content that you can easily copy and paste into a Word
document or any text editor and save it as needed.

**SQL Queries for Database Lab Exercise**

**Tables:**

* emp (EMPNO, ENAME, JOB, HIREDATE, SAL, MODE, DEPTNO)

* dept (DEPTNO, DNAME, LOCATION)

* stud\_master (REG\_NO, S\_NAME, BRANCH)

* stud\_detail (REG\_NO, COURSE\_CODE, COURSE\_NAME, MARKS, SEM)

**Queries:**

1. **Insert records into emp table:**

```sql
INSERT INTO emp (EMPNO, ENAME, JOB, HIREDATE, SAL, MODE, DEPTNO) VALUES

(1, 'John Doe', 'Manager', '2023-01-01', 50000, 'A', 10),

(2, 'Jane Smith', 'Clerk', '2023-02-15', 25000, 'B', 20),

(3, 'David Lee', 'Analyst', '2023-03-20', 40000, 'A', 10);

-- Add more records as needed

```

2. **Insert records into dept table:**

```sql

INSERT INTO dept (DEPTNO, DNAME, LOCATION) VALUES

(10, 'Accounting', 'New York'),

(20, 'Research', 'Dallas'),

(30, 'Sales', 'Chicago');

-- Add more records as needed

```

3. **Insert records into stud\_master table:**

```sql

INSERT INTO stud_master (REG_NO, S_NAME, BRANCH) VALUES

(101, 'Alice Wonderland', 'Computer Engineering'),

(102, 'Bob The Builder', 'Civil Engineering'),

(103, 'Charlie Chaplin', 'Mechanical Engineering');

-- Add more records as needed

```
4. **Insert records into stud\_detail table:**

```sql

INSERT INTO stud_detail (REG_NO, COURSE_CODE, COURSE_NAME, MARKS, SEM)


VALUES

(101, 'CS101', 'Introduction to Programming', 90, 1),

(101, 'MA101', 'Calculus I', 85, 1),

(102, 'CE101', 'Structural Analysis', 78, 1),

(103, 'ME101', 'Thermodynamics', 92, 1);

-- Add more records as needed

```

5. **Select all information from emp table:**

```sql

SELECT * FROM emp;

```

6. **List all the employees who have salary between 1000 and 2000:**

```sql

SELECT * FROM emp WHERE SAL BETWEEN 1000 AND 2000;

```

7. **List names and jobs of all clerks in department 20:**


```sql

SELECT ENAME, JOB FROM emp WHERE JOB = 'Clerk' AND DEPTNO = 20;

```

8. **Display all the different job types:**

```sql

SELECT DISTINCT JOB FROM emp;

```

9. **List department numbers and names in department name order:**

```sql

SELECT DEPTNO, DNAME FROM dept ORDER BY DNAME;

```

10. **Select all information from stud\_master table:**

```sql

SELECT * FROM stud_master;

```

11. **Display Registration number and name of students whose department is “computer
engineering”:**
```sql

SELECT REG_NO, S_NAME FROM stud_master WHERE BRANCH = 'Computer


Engineering';

```

12. **Add a column “SPOUSE” to the emp table that will hold the name of an employee’s
spouse:**

```sql

ALTER TABLE emp ADD SPOUSE VARCHAR(255); -- Assuming a maximum length of 255
for the spouse's name

```

13. **Modify the job of employees to “programmer” whose job is “trainee”:**

```sql

UPDATE emp SET JOB = 'Programmer' WHERE JOB = 'Trainee';

```

14. **Delete record whose location is “Baroda” from dept table:**

```sql

DELETE FROM dept WHERE LOCATION = 'Baroda';

```

15. **Drop a table “stud\_master”:**


```sql

DROP TABLE stud_master;

```

16. **Create a table “ManagerHist” from emp whose job is “Manager”:**

```sql

CREATE TABLE ManagerHist AS SELECT * FROM emp WHERE JOB = 'Manager';

```

17. **Copy all the information of department 20 into the “ManagerHist” table:**

```sql

INSERT INTO ManagerHist SELECT * FROM emp WHERE DEPTNO = 20;

```

18. **Define: Join. Explain self join.**

* **Join:** A JOIN clause is used to combine rows from two or more tables, based on a
related column between them.

* **Self Join:** A self join is a join operation where a table is joined with itself. It's useful
for querying hierarchical data or comparing rows within the same table.

19. **Retrieve employee number, employee name and their department name, in
department name order:**

```sql
SELECT e.EMPNO, e.ENAME, d.DNAME

FROM emp e

JOIN dept d ON e.DEPTNO = d.DEPTNO

ORDER BY d.DNAME;

```

20. **Show all employee details who lives in Baroda.**

```sql

--This assumes there's a location for employees. If not, and you meant department
location:

SELECT e.* FROM emp e

JOIN dept d ON e.DEPTNO = d.DEPTNO

WHERE d.LOCATION = 'Baroda';

```

21. **Display the name, salary and department number of employees whose salary is more
than 10000:**

```sql

SELECT ENAME, SAL, DEPTNO FROM emp WHERE SAL > 10000;

```

22. **List the employee name, job, salary and department name for everyone in the
company except clerks:**

```sql
SELECT e.ENAME, e.JOB, e.SAL, d.DNAME

FROM emp e

JOIN dept d ON e.DEPTNO = d.DEPTNO

WHERE e.JOB <> 'Clerk';

```

23. **Sort on salary displaying the highest salary first:**

```sql

SELECT * FROM emp ORDER BY SAL DESC;

```

24. **List all employees by name and number along with their manager’s name and
number:**

```sql

--This requires that the emp table has a manager ID (e.g., MGR). Assuming it does:

SELECT e.ENAME as EmployeeName, e.EMPNO as EmployeeNumber, m.ENAME as


ManagerName, m.EMPNO as ManagerNumber

FROM emp e

LEFT JOIN emp m ON e.MGR = m.EMPNO; -- Assuming MGR is the manager's employee
number

```

25. **Display all the employees who earn less than their managers:**

```sql
--Again, assuming an MGR column in the emp table:

SELECT e.ENAME

FROM emp e

JOIN emp m ON e.MGR = m.EMPNO

WHERE e.SAL < m.SAL;

```

You might also like