SQL Workshop
SQL Workshop
for Beginners
by Thoufiq Mohammed (techTFQ)
on Lighthall.co
Contents
• Understanding Data, Database and RDBMS
• Introduction to SQL
• Understanding how data is stored in Relational Database
• SQL Commands
o Creating and modifying database object using DDL
o Loading and modifying data in the database using DML
o DCL and TCL commands
• SQL Query Writing
o Understanding SELECT, FROM, WHERE clause.
o Using different operators in SQL query.
o CASE, DISTINCT, ORDER BY, LIMIT and Inbuild functions.
o Group by and Having clause
o Aggregate functions
• Solving Assignment SQL Queries
Data
• Anything & Everything can be considered as data.
• Any information or fact can be considered as data.
• For an insurance company, details related to its employees,
customers, their products or even their address can be data.
• Data can be in any form such as a text message or an image
or a video or a document or single character itself.
Database
• Container which is filled with data or information which is
electronically stored in a computer system.
• Can in any form can be stored in the database.
• All companies use some sort of database to store their data.
• Purpose of storing data in the database is:
Easily Accessed
Modified
Protected
Analyzed
RDBMS vs
Database
SQL
• Structured Query Language (SQL). Also referred to as “SEQUEL”
• SQL is a programming language used to interact with Relational
Database Management System.
• SQL is primarily used in RDBMS.
• All major RDBMS like Oracle, MySQL, MSSQL, PostgreSQL etc use
SQL.
• Using SQL, you can:
Read data from a RDBMS
Write data into a RDBMS
Create, modify or delete database
Analyze data
Build reports
How data • In RDBMS, data is stored in multiple
is stored in tables.
MSSQL
DDL
Commands • Data Definition Language
• Used to define the structure of a database object
such as Table, Views, Procedure, Function etc.
• Commands include:
CREATE
ALTER
DROP
TRUNCATE
Data Types:
• INT
• VARCHAR
• DATE
• FLOAT
• BOOLEAN
Constraint
s
• CHECK constraint allows you to control the values that can be inserted into a column.
• By applying NOT NULL constraint on a column, you make sure that this column will never have
NULL values.
• UNIQUE constraint makes sure that the values in your column are always unique. It can have NULL
values.
• A table can only have one PRIMARY KEY constraint. Primary key constraint can either be applied
to a single column or to a combination of different columns. If a column (or if a combination of
multiple columns) is defined as a primary key, then this column/columns will always have unique
values across all the rows of that table. Also, primary key column cannot have null values hence
primary key can be treated as a combination of UNIQUE and NOT NULL constraints.
• FOREIGN KEY constraint can be used to form relation between tables. It basically helps to create a
master child relation between 2 tables.
DML
Commands • Data Manipulation Language
• DML commands are used to load, modify and
remove data from the database.
• Commands include:
INSERT
UPDATE
DELETE
MERGE
DQL • Data Query Language
command
• Includes only the SELECT statement
• Can be used to read data from one or multiple tables.
• Contains following clauses:
SELECT – list of columns
FROM – list of tables
WHERE – filter conditions and join conditions
• Can also include following clauses:
GROUP BY – Grouping of data based on specified
column(s)
HAVING – Filter condition for the grouped data
SQL • JOINS
SQL 3.
never failed and have never done any claims.
Find the benefit whose policy was issued prior to risk
Queries
commencement.
4. Which are the products currently not taken by inforce policies.
5. Find the policy and its status with the least sum assured
6. Fetch the policy id, benefit id, policy status and product name of
Using below tables:
all policies who are not inforce.
• REFERENCE_LOOKUP 7. Find the policies who do not have any benefits.
• TRANSACTION_MASTER
• 8. Find the oldest policy with at least one benefit.
BENEFIT_MASTER
• POLICY_MASTER 9. Find all the claims transaction that belong to Health Insurance
• PRODUCT_MASTER product.
*SQL Scripts provided separately 10. Write a SQL query to find the details of the product which has
sold the most.
References:
• W3Schools
• PostgreSQL Tutorial
• My YouTube SQL Tutorial video
• Recommended SQL Course on LearnSQL.com
Thank you
Questions?