Skip to main content

Basic

1. What are the three normal forms of a database?

  1. First Normal Form (1NF)

    • Requires that each column in a database table contains indivisible atomic data items.

      Student IDNameFamily InformationSchool Information
      1John DoeParents: Mike, JaneSchool: XYZ High, Grade: A
      2Jane SmithParents: Bob, AliceSchool: ABC High, Grade: B
    • Here, the "Family Information" and "School Information" columns contain multiple pieces of data.

    • Adjusted Table (Satisfies 1NF):

      Student IDNameParent 1Parent 2School NameGrade
      1John DoeMikeJaneXYZ HighA
      2Jane SmithBobAliceABC HighB
  2. Second Normal Form (2NF)

    1. Eliminates partial dependencies.

    2. Building on 1NF, non-key attributes must fully depend on the entire primary key (eliminating partial functional dependencies of non-key attributes on the primary key).

    3. 2NF ensures that every column in a database table is related to the entire primary key, not just a part of it (primarily relevant for composite primary keys).

    • Table: CourseEnrollments (Before 1NF)

      StudentIDCourseCodeStudentNameCourseNameDepartmentNameEnrollmentDate
      S001C001Alice SmithDatabasesComputer Science2025-09-01
      S001C002Alice SmithCalculusMathematics2025-09-03
      S002C002Bob JonesCalculusMathematics2025-09-02
      • Primary Key: (StudentID, CourseCode) (composite key, as a student can enroll in multiple courses, and a course can have multiple students).
      • 2NF Violation: StudentName, CourseName, and DepartmentName depend on only part of the composite key, not the entire key (StudentID, CourseCode).
    • Adjusted Tables (Satisfy 2NF)

      • Split the table:

        StudentIDStudentName
        S001Alice Smith
        S002Bob Jones
        CourseCodeCourseNameDepartmentName
        C001DatabasesComputer Science
        C002CalculusMathematics
        StudentIDCourseCodeEnrollmentDate
        S001C0012025-09-01
        S002C0022025-09-02
        S001C0022025-09-03
  3. Third Normal Form (3NF):

    1. Ensure non-key attributes depend only on the primary key, not other non-key attributes

      EmployeeIDEmployeeNameDepartmentIDDepartmentNameDepartmentLocation
      E001Alice SmithD01MarketingNew York
      E002Bob JonesD02EngineeringSan Francisco
      E003Carol WhiteD01MarketingNew York
      • Adjusting to 3NF
      EmployeeIDEmployeeNameDepartmentID
      E001Alice SmithD01
      E002Bob JonesD02
      E003Carol WhiteD01
      DepartmentIDDepartmentNameDepartmentLocation
      D01MarketingNew York
      D02EngineeringSan Francisco

2. Table Joins in MySQL

  1. Inner Join (INNER JOIN)
SELECT employees.name, departments.name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.id;
  1. Left Outer Join (LEFT JOIN)
SELECT employees.name, departments.name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.id;
  1. Right Outer Join (RIGHT JOIN)
SELECT employees.name, departments.name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.id;
  1. Full Outer Join (FULL JOIN)
  • A full outer join returns all rows from both tables, including non-matching rows. In MySQL, FULL JOIN is not directly supported and must be implemented using UNION. Example:
SELECT employees.name, departments.name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.id

UNION

SELECT employees.name, departments.name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.id;