SQL Boot Camp
Business Analytics Leadership Council• 08.15.2020
Follow us on
Instagram!
@utd_msbuan
A collection of related
What is a Database? information that is stored
together
● Directory
● Course Catalog
● Convenience Store
Inventory List
Database
A special software/program that helps
Management users create and maintain a database
within an organization
System
● Easy to manage large amounts of information
● Uniform handling of security
● Backups and Versioning
● Uniform import/export from various data
sources
● Interact well w/ 3rd party applications and
APIs
Relational Databases Non Relational Databases
(RDBMS) (NoSQL)
● Uses a table structure to organize data
● Data organized in various ways (not tabular)
● Each table represents one ‘entity’ in a
database ● Ex. Key Value pairs (Document DB etc.)
● Each table has columns (attributes) and rows ● Graphical Databases
(records) w/ connections between tables
● Widely used in big data networks and social
● A unique key identifies each row media databases
● Most common type of database
Structured Query Language (SQL)
● Standardized Language for interacting with and querying data from a RDBMS
● Used to perform Create, Read, Update, Delete operations, plus other administrative
tasks on data
● SQL code is universal, it can be used across different DBMS without much modification
● Examples of Relational Databases: mySQL* , Oracle, postgreSQL, SQL Server, mariaDB,
etc.
2
Tables in a Relational Database
P_ID (1) PName Price Category Manufacturer
001 Gizmo 19.99 Gadget GizmoWorks
002 Powergizmo 29.99 Gadget GizmoWorks
1
003 SingleTouch 149.99 Photography Canon
004 MultiTouch 203.99 Household Hitachi
1. Rows: Represent each record in the table (ex. each item)
3
2. Columns: Represent each attribute regarding each record (ex. details about each
item)
3. Primary Key: Unique identifier for each record (ex. Product_ID)
Data Definition Language (DDL) Data Manipulation Language (DML)
● Used to create and modify database structure ● Used to manipulate the data present in the
and its contents database, as well as to load/delete records
within it
● Examples include:
○ CREATE ● Examples include:
○ ALTER ○ INSERT
○ DROP ○ UPDATE
○ RENAME ○ DELETE
○ TRUNCATE ○ SELECT
Data Control Language (DCL) Transaction Control Language (TCL)
● Deals with Permissions, rights and other ● Commands used to manage transactions in
database access controls the database
● Examples include: ● Examples include:
○ GRANT ○ COMMIT
○ REVOKE ○ ROLLBACK
○ SAVEPOINT
Defining Tables
SELECT
FROM
Basic framework for
WHERE
querying data from
GROUP BY RDBMS using SQL
HAVING
ORDER BY
INTWhole Numbers
DECIMAL (M,N) Decimal Numbers
VARCHAR(I) Text String
Common Data Types
BOOLEAN True/False (1/0) used in RDBMS
BLOB Binary Large Object (large Data)
DATE YYYY-MM-DD
TIMESTAMP YYYY-MM-DD HH:MM:SS
CREATE TABLE student(
Student_id INT PRIMARY KEY,
Name VARCHAR(40),
Major VARCHAR(40)
);
DESCRIBE student;
ALTER TABLE student ADD gpa DECIMAL;
ALTER TABLE student DROP COLUMN gpa;
DROP TABLE student;
Creating a Table
INSERT INTO student VALUES(1,’Jack’,’Biology’);
INSERT INTO student VALUES(2,’Kate’,’Sociology’);
INSERT INTO student VALUES(3,’Claire’);
INSERT INTO student VALUES(4,’Jack’,’Biology’);
INSERT INTO student VALUES(5,’Mike’,’Computer Science’);
SELECT * FROM student;
Inserting Data into a Table
CREATE TABLE student(
Student_id INT PRIMARY KEY AUTO_INCREMENT,
Name VARCHAR(40) NOT NULL,
Major VARCHAR(40)DEFAULT ‘undecided’
);
Creating a Table with Constraints
Querying Data
Use this time to review the sheet with the
tables and run the table creation script in
mySQL
Find all employees ordered by salary
Find first 5 employees from the table
Find forenames and surnames of all employees
Find all unique genders
Find all employees who are (1) female and (2) born after 1969 OR make over
$80000
Find all employees born between 1970 and 1975
Find all employees named Jim, Michael, Johnny, or David
Sample Queries on Data
Find all employees ordered by salary
- Select * from employee order by salary;
Find first 5 employees from the table
- Select * from employee LIMIT 5;
Find forenames and surnames of all employees
- Select first_name as ‘forename’, last_name as ‘surname’ from
employee;
Find all unique genders
- Select distinct sex from employee;
Find all employees who are (1) female and (2) born after 1969 OR make over $80000
- Select * from employee where (birth_day >= ‘1970-01-01’ AND sex =
‘F’) OR salary > 80000;
Find all employees born between 1970 and 1975
- Select * from employee where birth_day BETWEEN ‘1970-01-01’ AND
‘1975-01-01’;
Find all employees named Jim, Michael, Johnny, or David
- Select * from employee where first_name IN
(‘Jim’,’Michael’,’Johnny’,’David’);
Sample Queries on Data
Wildcards
A wildcard character is used to replace a
character(s) in any given string
It is used with the SQL ‘LIKE’ operator
% - Any number of characters
_ - One Character
Find any client who are an LLC
Find any branch suppliers in the paper business
Find any employee born on the 10th day of the month
Sample Queries using Wildcards
Find any client who are an LLC
- Select * from client where client_name like ‘%LLC’;
Find any branch suppliers in the paper business
- Select * from branch_supplier where supplier_name like
‘%Label%’;
Find any employee born on the 10th day of the month
- Select * from employee where birth_day like
‘______10%’;
Sample Queries using Wildcards
Joins
Combines the data from 2 or more tables
in a database using a connected attribute
● Primary keys in one table sometimes
secondary keys in others
● 4 types of joins:
○ Outer Join
○ Inner Join
○ Left Join
○ Right Join
Find each branch and the names of their branch managers
- Select
Employee.emp_id,
Employee.first_name,
Employee.last_name,
branch.branch_name
FROM employee LEFT JOIN branch
ON employee.emp_id = branch.mgr_id;
Sample Queries using Joins
Nested Queries
Queries within queries used to filter data
● Works from the inside query first
● Works within SELECT, FROM, WHERE
Find the names of all employees who sold at least 50,000 units
- Select
first_name,
last_name
FROM employee WHERE employee.emp_id IN (
Select
emp_id
FROM works_with WHERE total_sales > 50000);
Nested Query
Sample Queries using Joins
Additional Resources
● https://www.w3schools.com/sql/ - SQL Tutorial
● DataCamp SQL courses - Free for MSBA students until December
● CodeCademy SQL courses
Thank You!