0% found this document useful (0 votes)
5 views5 pages

Dbms 6

The document provides an overview of basic SQL queries, including SELECT, PROJECT, and JOIN operations with examples. It covers various query types such as retrieving employee details, using aliases, set operations, nested queries, and aggregate functions. Additionally, it outlines SQL query structure and the use of clauses like WHERE, GROUP BY, and ORDER BY.

Uploaded by

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

Dbms 6

The document provides an overview of basic SQL queries, including SELECT, PROJECT, and JOIN operations with examples. It covers various query types such as retrieving employee details, using aliases, set operations, nested queries, and aggregate functions. Additionally, it outlines SQL query structure and the use of clauses like WHERE, GROUP BY, and ORDER BY.

Uploaded by

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

Simple SQL Queries

- Basic SQL queries use SELECT, PROJECT, and JOIN operations.


- Example Query: Retrieve birthdate and address of employee named 'John'.

SELECT BDATE, ADDRESS


FROM EMPLOYEE
WHERE FNAME='John';
```

Examples of Simple SQL Queries


- Query 1: Retrieve name and address of all employees who work in the 'Research'
department.
```sql
SELECT FName, LName, Address
FROM Employee, Department
WHERE DName='Research' AND DNumber=DNo;
Explanation
SELECT FName, LName, Address: Specifies that the query should retrieve the first name
(FName), last name (LName), and address (Address) of employees.

2. FROM Employee, Department: Indicates the query will fetch data from two tables:
Employee and Department.

3. WHERE DName='Research': Filters the results to include only rows where the department
name (DName) is "Research".

4. AND DNumber=DNo: Ensures that only rows where the department number (DNumber from
Department) matches the department number (DNo in Employee) are included. This creates a
link between the Employee and Department tables.

In short,
The query retrieves the first name, last name, and address of all employees who work in the
"Research" department by joining the Employee and Department tables based on their
department number.

- Query 2: For every project located in ‘Naivasha', list the project number, the controlling
department number, and the department manager's last name, address, and birthdate..

SELECT PNumber, DNum, LName, BDate, Address


FROM Project, Department, Employee
WHERE DNum=DNumber AND MgrSSN=SSN AND PLOCATION='Naivasha';
```
Explanation
This SQL query retrieves information about projects located in "Naivasha" along with details
about the department and its manager. Here's what each part does:

1. SELECT PNumber, DNum, LName, BDate, Address:


Specifies the columns to retrieve:

PNumber: The project number.

DNum: The department number associated with the project.

LName: The last name of the department manager.

BDate: The birth date of the department manager.

Address: The address of the department manager.

2. FROM Project, Department, Employee:


Combines data from the Project, Department, and Employee tables.

3. WHERE DNum=DNumber:
Links the Project table to the Department table by matching the department number (DNum in
Project and DNumber in Department).

4. AND MgrSSN=SSN:
Links the Department table to the Employee table by matching the manager's SSN (MgrSSN in
Department and SSN in Employee).

5. AND PLOCATION='Naivasha':
Filters the results to include only projects where the project location (PLocation) is "Naivasha".

In short
The query retrieves the project number, department number, and the last name, birth date, and
address of the manager for all projects located in "Naivasha". It connects the Project,
Department, and Employee tables using the department number and manager's SSN.

Aliases, and DISTINCT, Empty WHERE-clause


- Aliases: Used to refer to the same relation twice.
For example, For each employee, retrieve the employee's name, and the name of his or her
immediate supervisor

```sql
SELECT E.Fname, E.LName, S.FName, S.Lname
FROM Employee AS E, Employee AS S
WHERE E.SuperSSN=S.SSN;
```
- Empty WHERE-clause: Selects all tuples.
```sql
SELECT SSN
FROM EMPLOYEE;
```
- Use of : Retrieve all attribute values.
```sql
SELECT FROM EMPLOYEE WHERE DNO=5;
```
- DISTINCT: Eliminates duplicate tuples.
```sql
SELECT DISTINCT SALARY FROM EMPLOYEE;
```

Set Operations
- Union, Minus, Intersect: Operate on union-compatible relations.
- Example: List project numbers involving 'Omondi'.
```sql
(SELECT PName
FROM Project, Department, Employee
WHERE DNUM=DNumber AND MGRSSN=SSN AND LNAME='Omondi')
UNION
(SELECT PName
FROM Project, Works_On, Employee
WHERE PNumber=PNo AND ESSN=SSN AND LName='Omondi');
```

Nesting of Queries
- Example: Retrieve name and address of employees in 'Research' department using nested
query.
```sql
SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE
WHERE DNO IN
(SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME='Research');
```

Explicit Sets
- Example: Retrieve SSNs of employees working on projects 1, 2, or 3.
```sql
SELECT DISTINCT ESSN FROM WORKS_ON WHERE PNO IN (1, 2, 3);
```

Substring Comparison
- LIKE operator: Used for partial string comparison.
```sql
SELECT FName, LName, Address
FROM Employee
WHERE Address LIKE '%Nairobi%';
```

Arithmetic Operations
- Example: Give 10% raise to employees on 'ProductX' project.
```sql
SELECT FNAME, LNAME, 1.1SALARY
FROM EMPLOYEE, WORKS_ON, PROJECT
WHERE SSN=ESSN AND PNO=PNUMBER AND PNAME='ProductX';
```

Ordering Query Results


- ORDER BY clause: Sorts query results.
```sql
SELECT DName, LName, FName, PName
FROM Department, Employee, Works_On, Project
WHERE DNumber=DNo AND SSN=ESSN AND PNO=PNumber
ORDER BY DName, LName, FName;
```

NULLs in SQL Queries


- IS/IS NOT: Used to compare NULL values.
```sql
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE SUPERSSN IS NULL;
```

Aggregate Functions
- COUNT, SUM, MAX, MIN, AVG: Used for aggregate calculations.
```sql
SELECT SUM(Salary), MAX(Salary), MIN(Salary), AVG(Salary)
FROM EMPLOYEE;
```

Grouping
- GROUP BY clause: Groups tuples for aggregate functions.
```sql
SELECT DNO, COUNT(), AVG(SALARY)
FROM EMPLOYEE
GROUP BY DNO;
```

Summary of SQL Queries


- SQL Query Structure:
```sql
SELECT <attribute list>
FROM <table list>
[WHERE <condition>]
[GROUP BY <grouping attribute(s)>]
[HAVING <group condition>]
[ORDER BY <attribute list>];
```

You might also like