Showing posts with label 151 ) sql DDL dml for finding the employees with sal more than manager. Show all posts
Showing posts with label 151 ) sql DDL dml for finding the employees with sal more than manager. Show all posts

Thursday, August 6, 2026

151 ) sql DDL dml for finding the employees with sal more than manager

 Below is DDL and DML for same 

### DDL (Create Table)


```sql

CREATE TABLE Employee (

    id INT PRIMARY KEY,

    name VARCHAR(50),

    salary INT,

    managerId INT

);


 


### DML (Insert Data)


```sql

INSERT INTO Employee (id, name, salary, managerId) VALUES 

(1, 'Joe', 70000, 3),

(2, 'Henry', 80000, 4),

(3, 'Sam', 60000, NULL),

(4, 'Max', 90000, NULL);


--

select * from employee

----------------------------------- # Method 1  : using joins  ---------

SELECT     e1.name AS Employee

FROM     Employee e1

JOIN     Employee e2 ON e1.managerId = e2.id

WHERE     e1.salary > e2.salary;

    

----------------------------------- # Method 2 : using  SUBQUERY ---------

SELECT     name AS Employee

FROM     Employee e

WHERE     salary > (

        SELECT salary  FROM Employee m 

        WHERE m.id = e.managerId

    );