0% found this document useful (0 votes)
2 views

queries

The document outlines SQL commands to create two tables, 'dept1' for departments and 'employ1' for employees, including their relationships. It includes data insertion for departments and employees, as well as various SQL queries to retrieve employee counts by department, employee names based on specific criteria, and minimum salaries per department. Additionally, it demonstrates how to aggregate and filter employee data using SQL syntax.

Uploaded by

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

queries

The document outlines SQL commands to create two tables, 'dept1' for departments and 'employ1' for employees, including their relationships. It includes data insertion for departments and employees, as well as various SQL queries to retrieve employee counts by department, employee names based on specific criteria, and minimum salaries per department. Additionally, it demonstrates how to aggregate and filter employee data using SQL syntax.

Uploaded by

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

CREATE TABLE dept1 (

dept_no INT PRIMARY KEY,


dept_name VARCHAR(100) NOT NULL
);

CREATE TABLE employ1(


emp_id number PRIMARY KEY,
emp_name VARCHAR(100),
dept_no number,
salary number,
FOREIGN KEY (dept_no) REFERENCES dept1(dept_no)
);

INSERT INTO dept1 (dept_no, dept_name)


VALUES
(101, 'HR'),
(102, 'IT'),
(103, 'Sales'),
(104, 'Finance'),
(105, 'Marketing');

INSERT INTO employee (emp_id, emp_name, dept_no, salary)


VALUES
(1, 'Alice', 101, 50000),
(2, 'Bob', 102, 60000),
(3, 'Charlie', 103, 55000),
(4, 'David', 101, 48000),
(5, 'Eve', 104, 60000),
(6, 'Frank', 102, 62000);

d.SELECT d.dept_no, d.dept_name, COUNT(e.emp_id) AS no_of_employees


FROM dept3 d
LEFT JOIN employ3 e ON d.dept_no = e.dept_no
GROUP BY d.dept_no, d.dept_name;

dept_no dept_name no_of_employees


101 HR 2
102 IT 2
103 Sales 1
104 Finance 1
105 Marketing 0

e.SELECT emp_name
FROM employ3
WHERE emp_name IN (
SELECT emp_name
FROM employ3
WHERE emp_name LIKE 'c%'
);

SELECT emp_name
FROM employ3
WHERE emp_name IN (
SELECT emp_name
FROM employ3
WHERE emp_name LIKE 'A%' OR emp_name LIKE 'C%'
);
EMP_NAME
--------------------------------------------------------------------------------
Bob
charlie

f.select emp_name from employ3 where (select max(salary) from employ3)>5000;

EMP_NAME
--------------------------------------------------------------------------------
Alice
Bob
charlie
David
Eve
Frank

6 rows selected.

a.select Emp.emp_name,avg(emp.salary) from employ3 emp where dept_no=102 Group By


emp_name;

EMP_NAME
--------------------------------------------------------------------------------
AVG(EMP.SALARY)
---------------
Bob
60000

Frank
62000

b.SELECT e.emp_name, e.salary, d.dept_name


FROM employ3 e, dept3 d
WHERE e.dept_no = d.dept_no
AND (e.dept_no, e.salary) IN (
SELECT dept_no, MIN(salary)
FROM employ3
GROUP BY dept_no
);

EMP_NAME
--------------------------------------------------------------------------------
SALARY
----------
DEPT_NAME
--------------------------------------------------------------------------------
Bob
60000
IT

charlie
55000
Sales

EMP_NAME
--------------------------------------------------------------------------------
SALARY
----------
DEPT_NAME
--------------------------------------------------------------------------------

David
48000
HR

Eve
62000

EMP_NAME
--------------------------------------------------------------------------------
SALARY
----------
DEPT_NAME
--------------------------------------------------------------------------------
Finance

c.SELECT e.dept_no, d.dept_name, COUNT(e.emp_id) AS no_of_employees


FROM employ3 e
JOIN dept3 d ON e.dept_no = d.dept_no
GROUP BY e.dept_no, d.dept_name;

DEPT_NO
----------
DEPT_NAME
--------------------------------------------------------------------------------
NO_OF_EMPLOYEES
---------------
101
HR
2

102
IT
2

DEPT_NO
----------
DEPT_NAME
--------------------------------------------------------------------------------
NO_OF_EMPLOYEES
---------------

103
Sales
1

104
Finance

DEPT_NO
----------
DEPT_NAME
--------------------------------------------------------------------------------
NO_OF_EMPLOYEES
---------------
1

You might also like