0% found this document useful (0 votes)
20 views26 pages

HR Schema Queries

The document covers a session on Basic SQL and Views, detailing SQL components such as DDL, DML, DCL, and TCL. It includes syntax and examples for creating, altering, and dropping tables, as well as various SQL queries for data manipulation. Additionally, it addresses the importance of views in terms of data integrity, usability, and security issues.

Uploaded by

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

HR Schema Queries

The document covers a session on Basic SQL and Views, detailing SQL components such as DDL, DML, DCL, and TCL. It includes syntax and examples for creating, altering, and dropping tables, as well as various SQL queries for data manipulation. Additionally, it addresses the importance of views in terms of data integrity, usability, and security issues.

Uploaded by

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

08:1

Basic SQL and Views

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

 DCL – Data Control Language

 TCL – Transaction Control Language

Database Systems
SQL Review- DDL (Data Definition Language)
 CREATE - build Object(table)

 ALTER - change Object (table)

 DROP - delete Object (table)

 TRUNCATE - remove all records from a table

 RENAME - rename an object

Database Systems
08:1
SQL Review- DDL (Data Definition Language) 7

CREATE TABLE
SYNTAX:

CREATE [OR REPLACE] TABLE <tableName>


(
attribute dateType,
.
.
CONSTRAINT constraint_name ..
);

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

• DROP TABLE student PURGE;


• Table and the memory allocated is cleared
• No way to recover

• DROP TABLE student CASCADE constraints;


• Table is deleted
• All referential integrity constraints with the table are also dropped

Database Systems
SQL Review- DML (Data Manupulation
Language)

INSERT - add data


UPDATE - change data
DELETE - remove data
SELECT - query data

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.

• SELECT department_id, AVG(salary) AS avg_salary


FROM employees GROUP BY department_id
HAVING AVG(salary) > 60000 ORDER BY avg_salary
DESC;

You might also like