SQL
SQL
Below
are examples of different types of SQL queries commonly used to perform tasks like retrieving data,
modifying data, and managing the database structure.
1. SELECT Query
sql
Copy code
sql
Copy code
sql
Copy code
sql
Copy code
sql
Copy code
2. INSERT Query
sql
Copy code
sql
Copy code
UPDATE Employees
4. DELETE Query
sql
Copy code
5. JOIN Query
A JOIN statement is used to combine rows from two or more tables based on a related column.
sql
Copy code
FROM Employees
6. GROUP BY Query
The GROUP BY statement is used to group rows with the same values and often used with aggregate
functions like COUNT(), SUM(), AVG(), MAX(), MIN().
sql
Copy code
FROM Employees
GROUP BY Department;
7. HAVING Query
The HAVING clause is used to filter records after a GROUP BY, similar to WHERE, but for aggregate
functions.
sql
Copy code
FROM Employees
GROUP BY Department
8. Aggregate Functions
sql
Copy code
sql
Copy code
9. Subqueries
sql
Copy code
sql
Copy code
sql
Copy code
sql
Copy code
The CREATE TABLE statement is used to create a new table in the database.
sql
Copy code
EmployeeName VARCHAR(100),
Department VARCHAR(50),
Salary DECIMAL(10, 2)
);
sql
Copy code
The UNION operator is used to combine the result sets of two or more SELECT statements. Duplicate
records are removed by default.
UNION
The LIMIT (or TOP in some databases) is used to specify the number of records to return.
sql
Copy code
These examples provide a basic understanding of SQL queries for various database operations. SQL is
highly versatile, and advanced features like window functions, stored procedures, and triggers allow
even more powerful querying and database management.
Normalization in DBMS
Normalization in DBMS (Database Management Systems) refers to the process of organizing data in a
database to minimize redundancy and avoid undesirable characteristics like insertion, update, and
deletion anomalies. The goal is to divide larger tables into smaller, related tables and ensure data
dependencies are logical.
There are several normal forms in DBMS, each representing a level of normalization. Below are the
different types of normalization with examples.
----------------------------------------
Normalized to 1NF:
-------------------------------
1 John Math
1 | John | Physics
2 | Alice | Chemistry
2 | Alice | Biology
Explanation: The courses were stored in a single cell, which is a violation of 1NF. After
normalization, each course is represented as a separate row.
o It is in 1NF.
o All non-key attributes are fully functionally dependent on the entire primary key (i.e.,
no partial dependency).
------------------------------------------------
Normalized to 2NF:
Student Table:
StudentID | StudentName
-----------------------
1 | John
2 | Alice
Course Table:
-------------------------------------
Explanation: The non-key attribute StudentName was dependent on just StudentID, causing
partial dependency. We split the table into two, separating student information from course
details.
o It is in 2NF.
o No transitive dependency exists (i.e., non-key attributes should not depend on other
non-key attributes).
-----------------------------------------------------------
StudentCourse Table:
StudentID | CourseID
--------------------
1 | 101
2 | 102
Course Table:
-------------------------------------
Instructor Table:
Instructor | InstructorPhone
----------------------------
StudentCourse Table:
StudentID | CourseID
--------------------
1 | 101
2 | 102
Course Table:
-------------------------------------
Instructor Table:
Instructor | InstructorPhone
----------------------------
o It is in 3NF.
---------------------------
Issue: The functional dependency Instructor → Room exists, but Instructor is not a superkey because
CourseID uniquely identifies rows.
Normalized to BCNF:
Course Table:
CourseID | Instructor
---------------------
InstructorRoom Table:
Instructor | Room
-----------------
Explanation: The dependency Instructor → Room violated BCNF because Instructor was not
a superkey. By splitting the table, we ensure that each non-key dependency relates directly
to a superkey.
o It is in BCNF.
o It contains no multi-valued dependencies (i.e., no attribute should have multiple
independent values for a single key).
----------------------------
1 | 101 | Soccer
1 | 101 | Music
2 | 102 | Painting
2 | 102 | Photography
Issue: A student can have multiple hobbies, creating a multi-valued dependency between StudentID
and Hobby.
Normalized to 4NF:
StudentCourse Table:
StudentID | CourseID
--------------------
1 | 101
2 | 102
StudentHobby Table:
StudentID | Hobby
-----------------
1 | Soccer
1 | Music
2 | Painting
2 | Photography
o It is in 4NF.
Non-5NF Table:
------------------------------
P1 | S1 | PA
P1 | S1 | PB
P1 | S2 | PA
ProjectSupplier Table:
ProjectID | SupplierID
----------------------
P1 | S1
P1 | S2
SupplierPart Table:
SupplierID | PartID
-------------------
S1 | PA
S1 | PB
S2 | PA
Explanation: We decomposed the table into two smaller tables, ensuring no data
redundancy and preserving the original information.
6. 5NF: No join dependencies; the table cannot be decomposed further without losing
information.
Normalization ensures that databases are efficient, organized, and free from redundant data,
reducing the chances of anomalies during data manipulation.