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

SQL Lecture Notes Compilation

The document provides detailed lecture notes on SQL basics, covering DBMS, SQL capabilities, and various SQL commands for creating and querying tables. It explains CRUD operations, handling NULL values, and sorting data, as well as different types of joins for retrieving data from multiple tables. Examples are provided for each concept to illustrate their application in SQL queries.
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)
1 views

SQL Lecture Notes Compilation

The document provides detailed lecture notes on SQL basics, covering DBMS, SQL capabilities, and various SQL commands for creating and querying tables. It explains CRUD operations, handling NULL values, and sorting data, as well as different types of joins for retrieving data from multiple tables. Examples are provided for each concept to illustrate their application in SQL queries.
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 Lecture Notes Compilation (Detailed

Version)
Lecture 05: SQL Basics and Simple Queries
Topics Covered:

 DBMS and Database Languages (DDL, DML, DCL)


 SQL Capabilities, Properties, and Statements
 Creating Tables and Populating Tables with SQL
 Querying Existing Tables: SELECT, WHERE, and ORDER BY Clauses
 Arithmetic Expressions, Column Aliases, and Handling NULL Values
 Sorting and Restricting Data Using SQL

1. Introduction to DBMS and Database Languages

A Database Management System (DBMS) is software that helps manage databases by enabling
users to create, modify, and query data efficiently. It ensures data consistency, security, and
integrity while allowing multiple users to interact with the data concurrently.

DBMS languages are categorized into three main types:

 Data Definition Language (DDL): Used to define and structure the database, including
creating and altering tables. Commands include CREATE, ALTER, and DROP.
 Data Manipulation Language (DML): Used for data retrieval and modification.
Commands include SELECT, INSERT, UPDATE, and DELETE.
 Data Control Language (DCL): Used to control access to the database. Commands
include GRANT and REVOKE.

2. SQL (Structured Query Language)

SQL is the standard language for interacting with relational databases. It allows users to perform
the following operations, known as CRUD operations:

 Create: Add new records to tables.


 Read: Retrieve records from tables.
 Update: Modify existing records.
 Delete: Remove records from tables.
SQL is a non-procedural language, meaning users specify what data they want to retrieve, not
how to retrieve it. This makes SQL close to natural language and easy to learn.

Example of a SQL Query:

SELECT * FROM Employees WHERE salary > 50000 ORDER BY hire_date DESC;

This query retrieves all employees with a salary greater than 50,000 and sorts the results in
descending order by their hire date.

3. Popular RDBMS (Relational Database Management Systems)

Two of the most commonly used RDBMS are:

 Oracle: A commercial multi-model database widely used for online transaction


processing (OLTP) and data warehousing. It supports various advanced features such as
in-database machine learning and blockchain tables.
 MySQL: An open-source RDBMS known for its reliability and performance. It supports
both SQL and NoSQL features and is commonly used for web applications.

4. Creating Tables and Populating Tables with SQL

To store data in a relational database, you need to create tables. Each table consists of columns
(fields) and rows (records).

4.1 Creating a Table

The CREATE TABLE statement is used to define a new table. You must specify the table name,
column names, and their data types.

Example:

CREATE TABLE Department (


deptNo INTEGER,
dName VARCHAR(20) UNIQUE NOT NULL,
dLoc VARCHAR(30) NOT NULL,
createDate DATE,
CONSTRAINT dept_pk PRIMARY KEY (deptNo)
);

In this example, the Department table has four columns:

 deptNo (integer) - Primary key


 dName (varchar) - Department name
 dLoc (varchar) - Department location
 createDate (date) - Date the department was created

4.2 Populating a Table

Once the table is created, you can insert records using the INSERT INTO statement.

Example:

INSERT INTO Department (deptNo, dName, dLoc, createDate)


VALUES (10, 'Database Management', 'New Cav', '2021-03-21');

This query inserts a new record into the Department table.

5. Querying Existing Tables Using SELECT Statements

The SELECT statement is used to retrieve data from a table. You can specify which columns to
retrieve and apply conditions to filter the results.

5.1 Selecting All Columns

SELECT * FROM Department;

This query retrieves all columns from the Department table.

5.2 Selecting Specific Columns

SELECT dName, dLoc FROM Department;

This query retrieves only the dName and dLoc columns.

6. Using Arithmetic Expressions in SELECT Statements

You can perform arithmetic operations on numeric columns in SQL queries.

Example:

SELECT empId, salary, salary + 1000 AS NewSalary FROM Employees;

This query retrieves the employee ID, current salary, and a new salary increased by 1,000.
7. Handling NULL Values

A NULL value represents missing or unknown data. It is important to handle NULL values
carefully in queries.

Example:

SELECT empId, salary, commPct FROM Employees WHERE commPct IS NULL;

This query retrieves employees who have no commission percentage specified.

8. Sorting Data Using ORDER BY

The ORDER BY clause is used to sort the query results.

Example:

SELECT empId, lName, hireDate FROM Employees ORDER BY hireDate DESC;

This query retrieves employees sorted by their hire date in descending order.

Lecture 06: SQL Join Queries (Detailed)


1. Introduction to Joins

Joins allow you to retrieve data from multiple tables by combining rows based on a related
column. There are several types of joins:

 Inner Join: Retrieves only the matching rows from both tables.
 Left Join: Retrieves all rows from the left table and matching rows from the right table.
 Right Join: Retrieves all rows from the right table and matching rows from the left table.
 Full Join: Retrieves all rows from both tables.

Inner Join:

An inner join retrieves records that have matching values in both tables. Only rows that satisfy
the join condition are returned in the result set.

Syntax:

SELECT columns
FROM Table1
INNER JOIN Table2 ON Table1.common_column = Table2.common_column;

Example:

SELECT E.empId, E.fName, E.lName, D.dName


FROM Employees E
INNER JOIN Department D ON E.deptNo = D.deptNo;

In this example, only employees who have a matching department in the Department table will
be included in the result set.

Left Join (Left Outer Join):

A left join retrieves all records from the left table and the matched records from the right table. If
there are no matches, the result will contain NULL values for the columns of the right table.

Syntax:

SELECT columns
FROM Table1
LEFT JOIN Table2 ON Table1.common_column = Table2.common_column;

Example:

SELECT D.dName, E.empId, E.fName


FROM Department D
LEFT JOIN Employees E ON D.deptNo = E.deptNo;

This query retrieves all departments and their associated employees. If a department has no
employees, NULL values are returned for employee details.

Right Join (Right Outer Join):

A right join is the opposite of a left join. It retrieves all records from the right table and the
matched records from the left table.

Syntax:

SELECT columns
FROM Table1
RIGHT JOIN Table2 ON Table1.common_column = Table2.common_column;

Example:

SELECT E.empId, E.fName, D.dName


FROM Employees E
RIGHT JOIN Department D ON E.deptNo = D.deptNo;
This query retrieves all employees along with their department names. If a department has no
associated employees, it will still be displayed.

Full Join (Full Outer Join):

A full join retrieves all records from both tables. If there are no matches, NULL values are
returned for the missing columns from each table.

Note: MySQL does not support the FULL OUTER JOIN clause directly. Instead, you can achieve
this by combining LEFT JOIN and RIGHT JOIN with the UNION operator.

Example:

SELECT E.empId, E.fName, D.dName


FROM Employees E
LEFT JOIN Department D ON E.deptNo = D.deptNo
UNION
SELECT E.empId, E.fName, D.dName
FROM Employees E
RIGHT JOIN Department D ON E.deptNo = D.deptNo;

In this example, all employees and departments are included in the result set, even if there are no
matches.

Self Join:

A self join is a regular join, but it is used to join a table to itself. This can be useful for
hierarchical data or finding relationships within the same table.

Syntax:

SELECT A.column_name, B.column_name


FROM Table A, Table B
WHERE A.common_column = B.common_column;

Example:

SELECT E1.empId, E1.fName, E2.fName AS Manager


FROM Employees E1
INNER JOIN Employees E2 ON E1.mgrId = E2.empId;

In this example, the query retrieves employees along with their respective managers.

You might also like