0% found this document useful (0 votes)
62 views33 pages

Database Course

Uploaded by

jason beryl
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)
62 views33 pages

Database Course

Uploaded by

jason beryl
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/ 33

Introduction to SQL

Course Instructor: Mike


Course Description: This course offers a comprehensive introduction to SQL
(Structured Query Language) for beginners. You'll learn the fundamentals of relational
databases, how to interact with them using SQL, and even explore basic database
design concepts.
Course Outline:
1. Introduction:
o What is a Database?
o Types of Databases
o Introduction to SQL
o Installing MySQL (relational database management system)
2. Writing SQL Queries:
o Creating Databases and Tables
o Inputting and Retrieving Information
3. Advanced Techniques:
o Extracting Specific Information from Databases
4. Database Design:
o Database Schemas (structure and relationships)

Introduction to Databases

Learning Objectives:
 Understand the fundamental concept of databases.
 Explore different types of databases and how they store data with examples.
 Gain a basic understanding of relational databases and SQL.
 Be introduced to non-relational databases and their data structures with
examples.
 Learn about database queries and their purpose.
What is a Database?
 A database is a collection of related information.
 Examples include:
o Phone book: Stores names, phone numbers (related information for
contacting people).
o Shopping list: Lists items you need to buy (related information for a
shopping trip).
o To-do list: Tasks you need to complete (related information for staying
organized).
o Social media user base: Stores user profiles, posts, and connections (all
related information about users).
 Databases can be stored electronically on computers (most common), on paper
(traditional phone books), or even memorized (limited use).
Why Use Databases on Computers?
 Computers excel at storing and managing large amounts of data efficiently.
 They provide features like security, easy backups, and data duplication for
redundancy.
 Example: Amazon.com needs to handle millions of user accounts, product
details, and purchase history. A computer database efficiently stores and
manages this information.
Database Management Systems (DBMS)
 A DBMS is a specialized software application designed to create, maintain, and
manage databases on computers.
 It simplifies handling large amounts of information by:
o Organizing data into structures like tables (explained later).
o Implementing security measures (e.g., passwords) to control access.
o Enabling data backups (for disaster recovery) and imports/exports from
other sources (data exchange).
o Interacting with software applications (e.g., websites like Amazon.com can
access and modify product data in the database).
CRUD Operations
 CRUD stands for Create, Read, Update, and Delete.
 These are the four fundamental operations performed on database information:
o Create: Add new entries to the database. (Example: Adding a new
product to an online store's database).
o Read: Retrieve existing information from the database. (Example: A
customer searching for a specific product on an online store website).
o Update: Modify existing information in the database. (Example: Updating
a customer's address in their account settings).
o Delete: Remove unwanted information from the database. (Example:
Deleting an old blog post from a website).
 A good DBMS should be able to perform all CRUD operations efficiently.
Types of Databases
1. Relational Databases
o Organize data into tables with rows and columns.
 Tables: Act like spreadsheets with specific categories (columns)
and individual entries (rows).
 Example: A "Customers" table might have columns for customer
ID, name, email address, and phone number. Each row represents
a single customer.
o Each table has a unique identifier (key) for each row (ensures no duplicate
entries).
o Structured Query Language (SQL) is the standardized language for
interacting with relational databases (covered in later courses).
o Popular relational database management systems (RDBMS) include
MySQL, Oracle, PostgreSQL, and MariaDB.
2. Non-Relational Databases
o Encompass any database that doesn't follow the relational model (tables,
rows, columns).
o Store data in various structures like:
 Documents (JSON, XML): Store data in a flexible format like key-
value pairs or nested objects. (Example: User information on a
social media platform can be stored as a JSON document).
 Graphs: Represent relationships between data points using nodes
(data points) and edges (connections). (Example: A social network
can use a graph database to show connections between users).
 Key-value pairs: Simple data storage with a unique key linked to a
specific value. (Example: An online shopping cart can use a key-
value store to keep track of products and their quantities).
o Examples of non-Relational databases include MongoDB, DynamoDB,
Apache Cassandra, and Firebase.
o No single standard language exists for interacting with non- relational
databases; each system typically uses its own language.
Database Queries
 A database query is a specific request made to the DBMS to retrieve information.
 As database structures become more complex (many tables with relationships),
queries become essential for efficiently extracting the desired data.
 Similar to a Google search, a database query specifies the information you need,
but it uses a specific language (SQL for relational databases).
 Example: A customer on a shopping website searches for "blue jackets." The
website interacts with the database using a query to find all products in the
"clothing" table where the "color" column value is "blue" and the "category"
column value is "jackets."
 Benefits of Database Queries:
o Allow for retrieval of specific data without needing to access the entire
database.
o Enable filtering and sorting of data based on various criteria.
o Can be used for complex data analysis by combining information from
multiple tables.
Conclusion
Key takeaways include:
 Databases store collections of related information.
 Computers are efficient for storing and managing large databases.
 DBMS simplifies database management (creation, maintenance, security).
 CRUD operations (Create, Read, Update, Delete) manipulate data.
 Two main database types exist: relational (uses SQL) and non-relational (various
structures).
 Database queries are used to request specific information from the database.
By understanding these core concepts, you have a solid base for further learning about
database creation, data storage methods (tables, documents, etc.), and data retrieval
techniques (queries).

Relational Database Concepts Explained (with Examples and Table Diagrams)

Learning Objectives:
 Understand the fundamental concept of relational databases.
 Explore how data is stored in relational databases using tables and diagrams.
 Gain a basic understanding of primary keys, foreign keys, and composite keys.
 See examples of how these concepts are applied in a company database
scenario.
What is a Relational Database?
A relational database is a collection of related data organized into tables. It allows for
efficient storage, retrieval, and manipulation of data.
Tables: The Building Blocks
 Tables are like spreadsheets with rows and columns.
 Rows (horizontal entries): Represent individual records or entries in the
database (e.g., a student, an employee).
 Columns (vertical sections): Represent specific attributes or characteristics of
the data (e.g., student name, employee ID).
 Each table focuses on a specific entity (e.g., "Students," "Employees").
Example: Student Table

Primary Keys: Ensuring Uniqueness


 A primary key is a column (or combination of columns) that uniquely identifies
each row in a table.
 It guarantees no duplicate entries exist. (Corrected "not" to "no")
 There can only be one primary key per table.
Example: In the Student table, "Student ID" is the primary key because it ensures no
two students have the same ID.
Types of Primary Keys:
 Surrogate Key: An artificial identifier with no real-world meaning (often an auto-
generated number). (e.g., Student ID)
 Natural Key: A real-world identifier that uniquely defines an entity (e.g., Social
Security Number - not ideal due to privacy concerns).
Foreign Keys: Building Relationships
 A foreign key is a column in one table that references the primary key of another
table.
 It establishes connections between related tables.
Example: Company Database
Imagine a company database with the following tables:
1. Employee Table
Employee Table

2. Branch Table

Branch Table

Explanation:
 "Manager ID" in the Branch table is a foreign key. It references the "Employee
ID" (primary key) in the Employee table, establishing a connection between
employees and the branches they manage.
Composite Keys: When One Isn't Enough
 A composite key is a primary key consisting of multiple columns used together to
uniquely identify a row.
Example: Branch Supplier Table
Imagine a table that tracks which suppliers provide products to which branches:

Explanation:
 In this example, both "Branch ID" and "Supplier Name" are required together to
uniquely identify a supplier for a specific branch. Neither column alone would be
sufficient.
Benefits of Relational Databases:
 Efficient data storage and retrieval.
 Enforces data integrity by preventing duplicate entries.
 Enables easy data manipulation through queries (covered in later courses).
 Creates flexible data models by establishing relationships between tables.
Conclusion
Relational databases provide a structured and organized way to store and manage
large amounts of data. Understanding tables, primary keys, foreign keys, and composite
keys is fundamental for working with relational databases.

Introduction to SQL (Structured Query Language)

Learning Objectives:
 Understand the fundamental concepts of SQL and its role in relational
databases.
 Gain an overview of the different functionalities of SQL.
 Learn basic query structure for retrieving data from a database.
What is SQL?
 SQL stands for Structured Query Language.
 It's a specialized language used to interact with relational databases.
 Unlike traditional programming languages, SQL focuses on querying,
manipulating, and managing data within relational database structures.
Relational Database Management Systems (RDBMS)
 Relational databases are organized collections of data stored in tables with rows
and columns.
 A Relational Database Management System (RDBMS) is a software application
used to create, maintain, and interact with these databases.
 SQL acts as the communication bridge between you and the RDBMS, allowing
you to perform various operations on the data.
Why Use SQL?
 Efficient data retrieval: SQL allows you to target specific data from vast amounts
of information stored within the database.
 Data manipulation: Insert, update, and delete operations on data can be
performed using SQL.
 Data control: Control access to the database by managing users and
permissions through SQL.
 Data definition: Define the structure of the database, including tables, columns,
and data types using SQL.
The Four Faces of SQL
SQL can be categorized into four main functionalities:
1. Data Query Language (DQL): Enables retrieval of data from the database
based on specific criteria. This is the most commonly used aspect of SQL.
2. Data Definition Language (DDL): Used to define the database schema, which
outlines the structure of the database. This includes creating tables, specifying
columns within tables, and defining the data types each column can hold.
3. Data Control Language (DCL): Focuses on managing user access and
permissions within the database. This ensures data security and controls who
can view, modify, or delete information.
4. Data Manipulation Language (DML): Allows you to modify the contents of the
database. This includes inserting new data, updating existing data, and deleting
unwanted data.
Understanding SQL Queries
A core concept of SQL is the query. A query is a structured instruction written in SQL
that tells the RDBMS exactly what data you want to retrieve.
Example Query:
Imagine a database table named "Employees" containing information about employees,
including their name, age, and salary. Here's a sample query to retrieve data:
SQL
SELECT employee_name, employee_age
FROM Employees
WHERE employee_salary > $30,000;

Explanation:
 SELECT: This keyword specifies the columns you want to retrieve from the table.

 employee_name, employee_age: These are the specific columns (attributes) we

want data from.


 FROM: This keyword indicates the table from which we want to retrieve data.

 Employees: This is the name of the table containing employee information.

 WHERE: This clause filters the results based on a specific condition.

 employee_salary > $30,000: This condition ensures the query only returns data

for employees with a salary greater than $30,000.


This example demonstrates how SQL queries can be used to target and retrieve
specific data from relational databases.
Conclusion
SQL is a powerful tool for interacting with relational databases. By understanding its
functionalities (DQL, DDL, DCL, DML) and basic query structure, you can effectively
manage and manipulate data stored within these systems.

Creating and Managing Tables in MySQL

I. Creating Tables
 CREATE TABLE Statement:
o Syntax: CREATE TABLE table_name (column1 data_type
[constraint1, constraint2, ...], column2 data_type

[constraint3, ...], ...);

 table_name: Choose a descriptive and unique name for your table.

 column: Each column represents a specific attribute of the data

you're storing.
 data_type: Defines the kind of information the column can hold

(explained in section III).


 constraint: Optional rules that govern the data in a column

(covered in section IV).


 Specifying Data Types:
o Common data types and their uses:
 INT: Stores whole numbers (e.g., student ID, age).

 DECIMAL(M, N): Stores numbers with decimals (e.g., GPA, price).

M represents the total number of digits allowed, and N represents


the number of digits allowed after the decimal point.
 VARCHAR(N): Stores variable-length strings (e.g., name, address). N

specifies the maximum number of characters allowed.


 CHAR(N): Stores fixed-length strings (e.g., initials, course code). All

characters within the specified length (N) are mandatory and filled
with spaces if necessary.
 DATE: Stores dates in YYYY-MM-DD format.
 DATETIME: Stores dates and times in YYYY-MM-DD HH:MM:SS

format.
 TEXT: Stores large amounts of text data.

 BLOB: Stores binary large objects (e.g., images, audio files).

 Setting Constraints:
o PRIMARY KEY: Uniquely identifies each row in a table. It's usually assigned

to an integer column that has unique values (e.g., student ID). A table can
only have one primary key.
o UNIQUE: Ensures that no duplicate values exist within a specific column or

set of columns.
o NOT NULL: Prevents null values from being inserted into a column.

o FOREIGN KEY: Creates a link between two tables, referencing a primary

key in another table.


 Example: Create a student table
SQL
CREATE TABLE student (
student_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
major VARCHAR(30),
gpa DECIMAL(3, 2),
enrollment_date DATE
);

II. Verifying Table Creation


 Use the DESCRIBE table_name statement to view the table's structure, including
column names, data types, and constraints.
SQL
DESCRIBE student;

III. Altering Tables


 Adding a New Column:
o Syntax: ALTER TABLE table_name ADD COLUMN column_name data_type
[constraint];

o Example: Add a phone_number column (varchar) to the student table:


SQL
ALTER TABLE student ADD COLUMN phone_number VARCHAR(15);
 Dropping a Column:
o Syntax: ALTER TABLE table_name DROP COLUMN column_name;
o Example: Remove the major column from the student table:
SQL
ALTER TABLE student DROP COLUMN major;

IV. Deleting Tables


 Use the DROP TABLE table_name statement to permanently remove a table.
SQL
DROP TABLE student;

V. Important Considerations
 Choose data types carefully, considering the type of data you'll be storing and the
amount of storage space required.
 Setting a primary key is essential for efficient data retrieval and manipulation.
 Use constraints like NOT NULL to ensure data integrity and prevent invalid entries.
 Foreign keys help establish relationships between tables, reducing data
redundancy and improving data consistency.
 Consider real-world scenarios when defining column lengths (e.g., 50 characters
for a name might be more practical than 20).
Additional Notes
 This guide focuses on fundamental data types and constraints. MySQL offers a
wider range depending on your specific needs.
 Explore additional data types like SET, ENUM, and TIME for specialized use cases.
 Always back up your data before making significant changes to your tables.
By understanding these concepts, you'll be well-equipped to create and manage
relational database tables in MySQL effectively.

Inserting Data into Database Tables


Basic Insertion:
1. Use the INSERT INTO statement followed by the table name and VALUES.
2. Inside parentheses after VALUES, specify the data to be inserted, in the same
order as the columns defined in the table.
3. Enclose string values in double quotes.
Example: Inserting data into a student table with columns student_id, name, and
major.
SQL
INSERT INTO student VALUES (1, "Jack", "Biology");

Inserting Selective Data:


 If a column allows null values and you don't have data for it, you can omit it
during insertion.
Example: Inserting a student with ID 3, name "Claire", but no major specified:
SQL
INSERT INTO student (student_id, name) VALUES (3, "Claire");
 This will result in "NULL" being inserted for the major column.
Additional Notes:
 Duplicate entries for the primary key are not allowed.
 The video mentions there are more advanced techniques for inserting data,
covered in future tutorials.

Database Insertion & Constraints

Inserting Information

 Basic Syntax:
o INSERT INTO table_name VALUES (value1, value2, ...);

o INSERT INTO table_name (column1, column2, ...) VALUES (value1,

value2, ...);

 Example: Inserting a student into a student table:


SQL
INSERT INTO student VALUES (1, 'Jack', 'Biology');
Creating Tables with Constraints

 NOT NULL:
o Ensures a column cannot have empty (NULL) values.
o Example: name VARCHAR(50) NOT NULL
 UNIQUE:
o Ensures all values in a column are different.
o Example: major VARCHAR(50) UNIQUE

Dropping & Recreating Tables

 DROP TABLE: Deletes an existing table.


o Example: DROP TABLE student;

Additional Constraints

 DEFAULT:
o Sets a default value if no value is provided during insertion.
o Example: major VARCHAR(50) DEFAULT 'Undecided'
 AUTO_INCREMENT (for Primary Keys):
o Automatically increments the primary key value with each new row.
o Example: student_id INT PRIMARY KEY AUTO_INCREMENT

Error Handling

 "Cannot be null" Error: Occurs when trying to insert a NULL value into a column
defined as NOT NULL.
 "Duplicate entry" Error: Occurs when trying to insert a duplicate value into a column
defined as UNIQUE.
Key Points

 Constraints help control the data entered into a database.


 A primary key is implicitly NOT NULL and UNIQUE.
 Use AUTO_INCREMENT to simplify primary key management.
 Pay attention to error messages to understand insertion issues.

key differences between primary keys and unique keys in a database context:

Feature Primary Key Unique Key

Uniquely identify each record


Purpose Prevent duplicate values in a column
in a table

Null Values
No Yes (multiples allowed)
Allowed

Number Allowed One per table Multiple per table

Automatic
Yes (usually clustered index) Yes (usually non-clustered index)
Indexing

Auto Increment Supported Not supported

Cannot be changed or Can be changed (with caution due to


Modification
deleted easily potential conflicts)

Example Use Student ID, Employee ID, Email address, Social Security
Cases Order Number Number, License Plate

Key Takeaway:
 Primary Key: The fundamental way to uniquely identify and reference each row
in a table. It's your go-to for reliable record identification.
 Unique Key: Primarily used to enforce data integrity by preventing duplicate
values within a specific column (with the exception of one NULL value). It's useful
when you want to ensure certain data points are distinct.
What is a Clustered Index?
A clustered index is a way of organizing the information in the database so that it’s
sorted in a specific order. This makes it easier and faster to find the information you
need.

Example:
Let's say the library has the following books:

 Book ID: 3, Title: "Harry Potter", Author: "J.K. Rowling", Published Date: "1997"
 Book ID: 1, Title: "The Hobbit", Author: "J.R.R. Tolkien", Published Date: "1937"
 Book ID: 2, Title: "1984", Author: "George Orwell", Published Date: "1949"

If the library creates a clustered index on the Book ID, the database will store these
books in the following order:

1. Book ID: 1, Title: "The Hobbit", Author: "J.R.R. Tolkien", Published Date: "1937"
2. Book ID: 2, Title: "1984", Author: "George Orwell", Published Date: "1949"
3. Book ID: 3, Title: "Harry Potter", Author: "J.K. Rowling", Published Date: "1997"

Updating and Deleting Rows in SQL

Updating Rows

 Basic Syntax:
o UPDATE table_name SET column1 = value1, column2 = value2, ...

WHERE condition;

 Examples:
o Update a specific column for all rows:
SQL
UPDATE student SET major = 'Bio';
o Update based on a condition:
SQL
UPDATE student SET major = 'Bio' WHERE major = 'Biology';

o Update multiple columns based on a condition:


SQL
UPDATE student SET name = 'Tom', major = 'Undecided' WHERE
student_id = 1;

o Update multiple columns with complex conditions (using OR):


SQL
UPDATE student SET major = 'Biochemistry' WHERE major = 'Bio'
OR major = 'Chemistry';

 Key Points:
o The WHERE clause is optional. If omitted, the update applies to all rows.
o Use logical operators like AND and OR to create more complex conditions.

Deleting Rows

 Basic Syntax:
o DELETE FROM table_name WHERE condition; (optional WHERE)

 Examples:
o Delete a specific row:
SQL
DELETE FROM student WHERE student_id = 5;

o Delete rows based on multiple conditions:


SQL
DELETE FROM student WHERE name = 'Tom' AND major =
'Undecided';

o Delete all rows:


SQL
DELETE FROM student;

 Key Points:
o Be cautious when deleting without a WHERE clause, as it removes all rows.
Important Note: Always double-check your UPDATE and DELETE statements before
executing them, especially when not using a WHERE clause, to avoid unintended data
loss.

Mastering the Art of Data Retrieval with SQL's SELECT Statement

This enhanced guide empowers you to wield the SELECT statement, a fundamental tool
in your SQL arsenal. It allows you to precisely extract the data you need from relational
databases.
Tailoring Your Data Selection:
 Retrieve everything: Use SELECT * to grab all columns from a table, but be
mindful of potential performance implications for large datasets.
 Pick specific data points: Specify individual column names for a more focused
approach (e.g., SELECT name, major, student_id).
 Maintain clarity: Prefix table names with a dot (.) to enhance readability (e.g.,
student.name).

Example:
SQL
SELECT student.name, student.major
FROM student;

This retrieves name and major columns from the student table, explicitly referencing the
table name for clarity.
Filtering for Specific Data (WHERE Clause):
 Target your results: Utilize the WHERE clause to filter data based on conditions.
 Comparison operators: Leverage operators like =, <, >, <> (not equal) to
compare values.
 Combining conditions: Link multiple conditions with AND (both true) or OR (at
least one true) for precise filtering.
 Multiple value comparisons: Use IN to efficiently check a column against a list
of values (e.g., major IN ('CS', 'Math')).
Example:
SQL
SELECT name
FROM student
WHERE major = 'Biology' AND student_id > 2
OR name = 'Mike';

This retrieves the name for students with major Biology and student ID greater than 2,
or for students named Mike.
Organizing Your Retrieved Data (ORDER BY Clause):
 Sort efficiently: Utilize the ORDER BY clause to arrange retrieved data in a
specific order.
 Specify sorting direction: Use ASC for ascending (default) or DESC for
descending order.
 Multi-column ordering: Sort based on multiple columns separated by commas
(e.g., ORDER BY major ASC, name DESC).
Example:
SQL
SELECT *
FROM student
ORDER BY major DESC, student_id ASC
LIMIT 10;

This retrieves all columns from the student table, ordered first by major (descending)
and then by student ID (ascending), limited to the top 10 results.
Limiting the Number of Results (LIMIT Clause):
 Control data volume: Restrict the number of retrieved rows with LIMIT.
Example:
SQL
SELECT name, major
FROM student
LIMIT 5;

This retrieves only the first 5 rows (students) from the student table, including name and
major columns.

Beyond the Basics:


 Comments: Use -- (two dashes) to add comments and enhance code
readability.
 Advanced Queries: Upcoming lessons will explore complex queries involving
multiple tables and joins.
Practice Makes Perfect:
Experiment with the student table! Try filtering, ordering, and limiting data using various
combinations of these clauses. This hands-on approach will solidify your understanding
of the SELECT statement's power and versatility.
Additional Tips:
 Consider using aliases for complex column names or expressions to improve
readability (e.g., SELECT student_name AS name).
 Explore functions for data manipulation within the SELECT statement (e.g.,
UPPER(name) to convert names to uppercase).

By mastering these core concepts, you'll be well on your way to crafting powerful and
efficient SQL queries to retrieve the exact data you need from your databases.

Practice Questions for Mastering SQL's SELECT Statement with Answers

Table: student

Column Name Data Type Description Constraints

student_id INT Unique identifier for each student PRIMARY KEY

name VARCHAR Student's full name NOT NULL

major VARCHAR Student's academic major NOT NULL

1. Filtering and Ordering:


Answers:
a) Retrieve all student names from the student table, ordered alphabetically in
descending order.
SQL
SELECT name
FROM student
ORDER BY name DESC;
b) Find all students with a major in Computer Science and order them by student
ID in ascending order.
SQL
SELECT *
FROM student
WHERE major = 'Computer Science'
ORDER BY student_id ASC;
c) List the names and majors of students with a student ID greater than 3. Order
the results by major (ascending) and then by name (descending).
SQL
SELECT name, major
FROM student
WHERE student_id > 3
ORDER BY major ASC, name DESC;
2. Combining WHERE Clause Conditions:
Answers:
a) Find all students who are either Biology majors or have the name Kate.
SQL
SELECT *
FROM student
WHERE major = 'Biology' OR name = 'Kate';
b) Retrieve the names and student IDs of students with a major other than
Chemistry.
SQL
SELECT name, student_id
FROM student
WHERE major <> 'Chemistry';
c) List all students who have a major in Biology or Chemistry and a student ID
less than or equal to 4.
SQL
SELECT *
FROM student
WHERE major IN ('Biology', 'Chemistry') AND student_id <= 4;
3. Using the IN Operator:
Answers:
a) Find all students whose majors are Biology, Chemistry, or Sociology.
SQL
SELECT *
FROM student
WHERE major IN ('Biology', 'Chemistry', 'Sociology');
b) Retrieve the names of students with student IDs of 1, 3, and 5.
SQL
SELECT name
FROM student
WHERE student_id IN (1, 3, 5);
c) List the names and majors of students with majors in a list stored in a variable
called chosen_majors (e.g., chosen_majors = ('CS', 'Math')).
Explanation:
The approach for using variables depends on your database system. Here are common
methods:
i) Prepared Statements (Recommended):
SQL
SELECT name, major
FROM student
WHERE major IN (?, ?); -- Placeholders for chosen_majors values
Assigning Values (Database System Specific):
 Refer to the explanation in the previous response for examples on assigning
values using prepared statements in MySQL & MariaDB and PostgreSQL.
ii) Stored Procedures (if supported):
Consult your database system's documentation for details on declaring and using
variables within stored procedures.
4. Selecting Specific Columns and Limiting Results:
Answers:
a) Retrieve only the name and major columns for the first 2 students in the
student table.
SQL
SELECT name, major
FROM student
LIMIT 2;
b) Select all columns from the student table, but limit the results to the top 5
students ordered by student ID descending.
SQL
SELECT *
FROM student
ORDER BY student_id DESC
LIMIT 5;
c) Grab just the student_id for students with a major of Biology.
SQL
SELECT student_id
FROM student
WHERE major = 'Biology';
Challenge Question:
Write a query that retrieves the names and majors of all students, but if a
student's name is Mike, display "Top Student" instead of their actual name (use a
CASE statement for conditional logic).
Answer:
SQL
SELECT
CASE WHEN name = 'Mike' THEN 'Top Student' ELSE name END AS
student_name,
major
FROM student;
This query uses a CASE statement to check if the name is 'Mike'. If it is, it displays "Top
Student" instead. Otherwise, it displays the actual name.

Company Database Schema Summary

This video introduced a complex company database schema that will be used
throughout the rest of the course for learning advanced SQL queries.

Employee Table:

emp_i first_na last_na birth_da se super_ branch_


salary
d me me te x id id

1967- 250,000.
100 David Wallace M NULL 1
11-17 00

Levinso 1961- 110,000.


101 Jan F 100 1
n 05-11 00

1964- 75,000.0
102 Michael Scott M 100 2
03-15 0

1971- 63,000.0
103 Angela Martin F 102 2
06-25 0

104 Kelly Kapoor 1980- F 55,000.0 102 2


02-05 0

1958- 69,000.0
105 Stanley Hudson M 102 2
02-19 0

1969- 78,000.0
106 Josh Porter M 100 3
09-05 0

1973- 65,000.0
107 Andy Bernard M 106 3
07-22 0

1978- 71,000.0
108 Jim Halpert M 106 3
10-01 0

Branch Table:

branch_id branch_name mgr_id mgr_start_date

1 Corporate 100 2006-02-09

2 Scranton 102 1992-04-06

3 Stamford 106 1998-02-13

Client Table:

client_id client_name branch_id

400 Dunmore Highschool 2

401 Lackawana Country 2

402 FedEx 3
403 John Daly Law, LLC 3

404 Scranton Whitepages 2

405 Times Newspaper 3

406 FedEx 2

Works_With Table:

emp_id client_id total_sales

105 400 55,000.00

102 401 267,000.00

108 402 22,500.00

107 403 5,000.00

108 403 12,000.00

105 404 33,000.00

107 405

Here's a breakdown of the tables and their relationships:

 Employee table stores employee information including supervisor and branch


assignment.
o Primary Key: emp_id (Employee ID)
o Foreign Keys:
 super_id: references emp_id in the Employee table (supervisor)

 branch_id: references branch_id in the Branch table (branch

assignment)
 Branch table stores information about the company branches and their
managers.
o Primary Key: branch_id (Branch ID)
o Foreign Key:
 mgr_id: references emp_id in the Employee table (manager)

 Client table stores client information and the branch they work with.
o Primary Key: client_id (Client ID)
o Foreign Key:
 branch_id: references branch_id in the Branch table (branch)

 Works_With table defines the relationship between employees and clients,


recording sales amounts.
o Composite Primary Key: (emp_id, client_id) (Employee ID and Client ID)
 Branch Supplier table stores information about branch suppliers, with a
composite primary key due to the need for both branch_id and supplier_name
for unique identification.

For a deeper understanding, refer to the instructor's recommendation to review the PDF
provided in the course description. This will help you visualize the connections between
the tables.

Enhanced Notes on Creating a Complex Database Schema in SQL

Introduction:
This document summarizes the key concepts from a video tutorial on building a
complex database schema in SQL. It focuses on the design, creation, and population of
tables within a company database.
Database Design:
 The schema consists of five interrelated tables:
o Employee: Stores employee information (ID, name, birthdate, sex, salary,
supervisor ID, branch ID).
o Branch: Stores branch details (ID, name, manager ID, manager start
date).
o Client: Holds client information (ID, name, branch ID).
o Works_With: Links employees to clients with their total sales (composite
primary key: employee ID, client ID).
o Branch_Supplier: Associates branches with suppliers and the type of
supplies provided (composite primary key: branch ID, supplier name).
Foreign Keys:
 Foreign keys ensure data consistency by referencing existing data in other
tables.
o Employee.branch_id references Branch.branch_id.

o Employee.super_id references Employee.emp_id (an employee can be a

supervisor).
o Client.branch_id references Branch.branch_id.

o Works_With.emp_id references Employee.emp_id.

o Works_With.client_id references Client.client_id.

o Branch_Supplier.branch_id references Branch.branch_id.

Table Creation:
 The tutorial demonstrates creating each table using SQL CREATE TABLE
statements, specifying:
o Column names and data types.
o Primary key constraints (unique identifier for each row).
o Foreign key constraints (references to existing tables and columns).
Data Insertion:
 Due to circular foreign key dependencies, a specific order is required for data
insertion:
1. Insert employees with a temporary value for branch_id.
2. Insert branches referencing the previously inserted employee IDs for
managers.
3. Update employee entries with the correct branch_id values.
 The tutorial showcases using SQL INSERT statements to populate each table with
sample data.
Data Verification:
 The video concludes by demonstrating how to verify data insertion using SQL
SELECT statements to retrieve information from the populated tables. This

ensures all data was inserted correctly.


Key Takeaways:
 Complex database schemas involve careful planning and design to establish
appropriate relationships between tables using foreign keys.
 The order of data insertion is crucial when dealing with circular foreign key
dependencies.
 SQL commands like CREATE TABLE, INSERT, and SELECT are essential for
building and managing database structures.
Additional Notes:
 Consider including diagrams to visually represent the tables and their
relationships.
 You can expand on specific points like data types or foreign key options (e.g., ON
DELETE CASCADE vs. ON DELETE SET NULL).

 Mention limitations of the provided information (e.g., sample data might not be
exhaustive).

Traffic Academy - SQL Select Statements Tutorial

Note: This video introduced basic SQL select statements using a sample company
database.
Key Concepts:
 SELECT: This clause retrieves data from a table.
 *SELECT : Retrieves all columns from a table.
 FROM: This clause specifies the table to retrieve data from.
 ORDER BY: This clause sorts the results based on a specified column
(ascending by default, use DESC for descending).
 LIMIT: This clause limits the number of returned rows.
 SELECT (column1, column2, ...): This clause specifies individual columns to
retrieve.
 AS: This keyword renames a column in the results.
 DISTINCT: This keyword returns only unique values from a column.
Examples:
 Get all employees:
SQL
SELECT * FROM employee;

 Get all clients:


SQL
SELECT * FROM client;

 Get all employees ordered by salary (descending):


SQL
SELECT * FROM employee ORDER BY salary DESC;

 Get all employees ordered by sex then name:


SQL
SELECT * FROM employee ORDER BY sex, first_name, last_name;

 Get the first five employees:


SQL
SELECT * FROM employee LIMIT 5;

 Get first and last names of all employees:


SQL
SELECT first_name, last_name FROM employee;

 Get first names as "name" and last names as "surname":


SQL
SELECT first_name AS name, last_name AS surname FROM employee;

 Find all distinct genders in the "employee" table:


SQL
SELECT DISTINCT sex FROM employee;

Additional Notes:
 The video mentions using as to alias columns for readability.
 The video also covers the DISTINCT keyword to get unique values.

Absolutely, here are the enhanced notes incorporating additional details from the
transcript:

SQL Functions for Data Analysis (Draft Academy Course Notes)

I. Understanding SQL Functions


 What are they? Reusable blocks of code designed for specific data manipulation
tasks.
 What can they do? Count data points, calculate averages, find sums, and more.
II. Essential SQL Functions
 COUNT(column_name): Counts the number of entries in a particular column.
o Example: SELECT COUNT(employee_id) FROM employee; (Finds the total
number of employees)
 AVG(column_name): Calculates the average value of a numerical column.
o Example: SELECT AVG(salary) FROM employee; (Calculates the average
employee salary)
 SUM(column_name): Calculates the total of the values in a numerical column.
o Example: SELECT SUM(salary) FROM employee; (Calculates the total
payroll)
III. Filtering Data with WHERE Clause
 Narrows down the data based on a specified condition.
o Example: SELECT COUNT(employee_id) FROM employee WHERE sex =
'female'; (Counts the number of female employees only)

IV. Advanced Analysis: Aggregation with GROUP BY


 Groups data by a chosen column and performs calculations on each distinct
group.
o Example 1 (Counting by Category): SELECT sex, COUNT(*) FROM
employee GROUP BY sex; (Counts the number of male and female

employees)
o Example 2 (Summing by Employee): SELECT employee_id,
SUM(total_sales) FROM work_shift GROUP BY employee_id;

(Calculates the total sales for each employee)


V. Key Points
 SQL functions and aggregation are powerful tools for in-depth data analysis
within relational databases.
 Experiment with various functions and practice writing queries to enhance your
comfort level with data manipulation in SQL.
VI. Additional Tips from the Video (Optional):
 Consider creating small prompts or problems to practice writing SQL queries.
 These prompts can help you explore and test functionalities of different SQL
functions.

You might also like