0% found this document useful (0 votes)
7 views6 pages

SQL Lectures Complete

Uploaded by

hooriyabutt0611
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)
7 views6 pages

SQL Lectures Complete

Uploaded by

hooriyabutt0611
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/ 6

SQL Complete Lecture Notes with Quiz

Answers and Practice Queries


Lecture 1: Retrieving Data Using the SQL SELECT Statement

**Definition:**
The SQL SELECT statement is used to fetch data from a database. It allows users to query
and retrieve specific data from one or more tables.

**Syntax:**
```sql
SELECT column1, column2, ...
FROM table_name;
```

**Expressions:**
- `*` – Selects all columns.
- `DISTINCT` – Removes duplicates.
- `AS` – Assigns an alias to a column.
- `||` – Concatenates strings.

**Example:**
```sql
SELECT first_name, last_name FROM employees;
SELECT DISTINCT department_id FROM employees;
SELECT first_name || ' ' || last_name AS full_name FROM employees;
```

**Quiz Answers & Practice Queries:**


1. Retrieve all employees:
```sql
SELECT * FROM employees;
```
2. Get unique job IDs:
```sql
SELECT DISTINCT job_id FROM employees;
```
Lecture 2: Restricting and Sorting Data

**WHERE Clause:**
Used to filter records based on a condition.

**ORDER BY Clause:**
Used to sort results in ascending (ASC) or descending (DESC) order.

**Comparison Operators:** =, <>, >, <, >=, <=


**Logical Operators:** AND, OR, NOT
**BETWEEN, IN, LIKE** – Advanced filtering

**Examples:**
```sql
SELECT * FROM employees WHERE salary > 5000;
SELECT * FROM employees WHERE department_id IN (10, 20);
SELECT * FROM employees WHERE last_name LIKE 'A%';
SELECT * FROM employees ORDER BY salary DESC;
```

Lecture 3: Using Single Row Functions to Customize Output

**Single Row Functions:**


Operate on one row at a time and return one result per row.

**Types:**
- Character Functions: `UPPER`, `LOWER`, `INITCAP`, `LENGTH`, `SUBSTR`
- Number Functions: `ROUND`, `TRUNC`, `MOD`
- Date Functions: `SYSDATE`, `MONTHS_BETWEEN`, `ADD_MONTHS`, `NEXT_DAY`

**Example:**
```sql
SELECT UPPER(first_name), LENGTH(last_name) FROM employees;
SELECT SYSDATE FROM dual;
```

Lecture 4: Using Conversion Functions and Conditional Expressions

**Conversion Functions:**
- TO_CHAR: Converts date or number to string.
- TO_NUMBER: Converts string to number.
- TO_DATE: Converts string to date.

**Conditional Expressions:**
- CASE: If-then-else logic
- DECODE: Similar to CASE but simpler syntax

**Examples:**
```sql
SELECT TO_CHAR(SYSDATE, 'DD-Mon-YYYY') FROM dual;
SELECT CASE department_id WHEN 10 THEN 'Admin' ELSE 'Other' END FROM employees;
SELECT DECODE(job_id, 'IT_PROG', 'Programmer', 'Other') FROM employees;
```

Lecture 5: Reporting Aggregated Data Using the Group Functions

**Group Functions:**
- COUNT, SUM, AVG, MAX, MIN

**GROUP BY Clause:**
Used to group rows sharing a property.

**HAVING Clause:**
Used to filter groups after aggregation.

**Examples:**
```sql
SELECT department_id, COUNT(*) FROM employees GROUP BY department_id;
SELECT department_id, AVG(salary) FROM employees GROUP BY department_id HAVING
AVG(salary) > 5000;
```

Lecture 6: Displaying Data from Multiple Tables

**Joins:**
Used to retrieve data from multiple tables based on related columns.

**Types:**
- INNER JOIN
- LEFT OUTER JOIN
- RIGHT OUTER JOIN
- FULL OUTER JOIN
- NON-EQUI JOIN

**Examples:**
```sql
SELECT e.first_name, d.department_name
FROM employees e JOIN departments d ON e.department_id = d.department_id;

SELECT e.first_name, j.job_title


FROM employees e JOIN jobs j ON e.job_id = j.job_id;
```

Lecture 7: Using Subqueries to Solve Queries

**Subqueries:**
A query nested inside another query.

**Types:**
- Single-row
- Multiple-row
- Correlated Subquery

**Examples:**
```sql
SELECT first_name FROM employees WHERE salary > (SELECT AVG(salary) FROM
employees);
SELECT department_id FROM departments WHERE location_id = (SELECT location_id FROM
locations WHERE city = 'London');
```

Lecture 8: Using the Set Operators

**Set Operators:**
- UNION: Combines results and removes duplicates.
- UNION ALL: Includes duplicates.
- INTERSECT: Common rows.
- MINUS: Rows in first query but not in second.
**Rules:**
- Same number of columns
- Same data types

**Example:**
```sql
SELECT employee_id FROM employees WHERE department_id = 10
UNION
SELECT employee_id FROM employees WHERE department_id = 20;
```

Lecture 9: Manipulating Data

**DML Commands:**
- INSERT: Adds data.
- UPDATE: Modifies data.
- DELETE: Removes data.

**Examples:**
```sql
INSERT INTO employees (employee_id, first_name, salary) VALUES (300, 'Ali', 6000);
UPDATE employees SET salary = salary + 500 WHERE employee_id = 300;
DELETE FROM employees WHERE employee_id = 300;
```

Lecture 10: Using DDL Statements to Create and Manage Tables

**DDL Commands:**
- CREATE: Creates schema objects.
- ALTER: Modifies structure.
- DROP: Deletes structure.
- RENAME: Renames object.

**Constraints:** NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK

**Examples:**
```sql
CREATE TABLE test (
id NUMBER PRIMARY KEY,
name VARCHAR2(50) NOT NULL
);

ALTER TABLE test ADD (age NUMBER);


DROP TABLE test;
```

Lecture 11: Creating Other Schema Objects

**Schema Objects:**
- SEQUENCES: Auto-generate numbers.
- SYNONYMS: Aliases for objects.
- INDEXES: Improve performance.
- VIEWS: Virtual tables from queries.

**Examples:**
```sql
CREATE SEQUENCE emp_seq START WITH 1 INCREMENT BY 1;
CREATE VIEW emp_view AS SELECT first_name, salary FROM employees;
CREATE INDEX emp_idx ON employees(last_name);
CREATE SYNONYM emp FOR employees;
```

You might also like