HR Schema Queries
HR Schema Queries
Databases Systems
What we cover in this session
Review SQL
DDL – Data Definition Language
DML – Data Manipulation Language
View
Syntax and uses
Data Integrity/ usability and Security Issues
View Materialization
Database Systems
SQL Review- DDL (Data Definition Language)
CREATE - build Object(table)
Database Systems
08:1
SQL Review- DDL (Data Definition Language) 7
CREATE TABLE
SYNTAX:
Database Systems
SQL Review- DDL (Data Definition Language)
CREATE TABLE
CREATE TABLE STUDENTS (
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
PRIMARY KEY (ID)
);
Database Systems
08:1
7
SQL Review- DDL (Data Definition Language)
ALTER TABLE – example to add column
Syntax:
ALTER TABLE <table_name>
ADD column_name data_type constraint;
Database Systems
08:1
7
SQL Review- DDL (Data Definition Language)
ALTER TABLE – example to add column
Syntax:
ALTER TABLE student
ADD biometricID VARCHAR(10) NOT NULL;
Database Systems
08:1
7
SQL Review- DDL (Data Definition Language)
DROP TABLE
Syntax:
DROP TABLE <tableName>
[ CASCADE CONTRAINTS]
[PURGE];
Database Systems
08:1
7
SQL Review- DDL (Data Definition Language)
DROP TABLE – Example
• DROP TABLE student;
• Table is deleted
Database Systems
SQL Review- DML (Data Manupulation
Language)
Database Systems
SQL DML - SELECT
Six clauses
SELECT <Column List>
FROM <Table>
[WHERE <Condition>]
[GROUP BY <Column List>]
[HAVING <Condition>]
[ORDER BY <Column List>]
Database Systems
Q1
• (Retrieve employees in the 'IT' department with
salaries greater than 5000.)
• SELECT employee_id, first_name, last_name,
salary, department_id FROM employees WHERE
department_id = (SELECT department_id FROM
departments WHERE department_name = 'IT')
AND salary > 5000;
Q2
• Count the number of employees in each
department.
• SELECT department_id, COUNT(*) AS
total_employees FROM employees GROUP BY
department_id;
Q3
• Calculate total salary expenses for each
department.
• SELECT department_id, SUM(salary) AS
total_salary FROM employees GROUP BY
department_id;
Q4
• Find departments with an average salary
greater than 7000.
• SELECT department_id, AVG(salary) AS
avg_salary FROM employees GROUP BY
department_id HAVING AVG(salary) > 7000;
Q5
• Retrieve employees sorted by department and
then by salary in descending order.
• SELECT employee_id, first_name, last_name,
department_id, salary FROM employees
ORDER BY department_id ASC, salary DESC;
Q6
• Find jobs with more than 3 employees.
• SELECT job_id, COUNT(*) AS num_employees
FROM employees GROUP BY job_id HAVING
COUNT(*) > 3;
• Find departments where the average salary is
above $60,000, and list them in descending order
of average salary.