CREATE TABLE DEPT (
dept_no INT PRIMARY KEY,
dept_name VARCHAR(50),
location VARCHAR(50)
);
INSERT INTO DEPT VALUES (10, 'Sales', 'Mumbai');
INSERT INTO DEPT VALUES (20, 'HR', 'Pune');
INSERT INTO DEPT VALUES (30, 'IT', 'Nashik');
CREATE TABLE EMP (
emp_no INT PRIMARY KEY,
emp_name VARCHAR(50),
dept_no INT,
salary DECIMAL(10, 2),
manager_id INT,
FOREIGN KEY (dept_no) REFERENCES DEPT(dept_no),
FOREIGN KEY (manager_id) REFERENCES EMP(emp_no)
);
INSERT INTO EMP VALUES (103, 'Sumit Patil', 10, 80000, NULL);
INSERT INTO EMP VALUES (105, 'Ravi', 20, 70000, NULL);
INSERT INTO EMP VALUES (101, 'Nikhil', 10, 50000, 103);
INSERT INTO EMP VALUES (102, 'Amit', 10, 45000, 103);
INSERT INTO EMP VALUES (104, 'Sneha', 20, 40000, 105);
SELECT e.emp_no, e.emp_name, e.dept_no, d.location
FROM EMP e
JOIN DEPT d ON e.dept_no = d.dept_no
WHERE e.emp_name = 'Nikhil';
SELECT e.emp_no, e.emp_name, d.dept_name
FROM EMP e
JOIN DEPT d ON e.dept_no = d.dept_no
WHERE d.dept_name = 'Sales';
SELECT e.emp_no, e.emp_name, d.dept_name
FROM EMP e
JOIN DEPT d ON e.dept_no = d.dept_no
WHERE d.dept_name <> 'Sales';
SELECT e.emp_name, e.salary
FROM EMP e
JOIN EMP m ON e.manager_id = m.emp_no
WHERE m.emp_name = 'Sumit Patil';