SQL102
SQL102
What to expect
Till now, you have downloaded and installed MySQL and have a good idea about
creating tables, inserting data into it, retrieving data from it. You also have studied about
relationships between different tables in a dataset along with the data types and
constraints. You have learnt about primary and foreign keys which helps you define
relationships between different tables.
The CREATE DATABASE statement is used The DROP DATABASE statement is used to
to create a new SQL database drop an existing SQL database
After successfully running the above query After successfully running the above query
new database naming studentInformation existing database naming
will be created studentInformation will be dropped i.e.
A database may contain one or more deleted
tables
Importing a file
Go to Navigator window at left -> Schemas -> select the database under which you want
to import file -> Right click on Tables and then follow the following steps to import a file
from your system.
Click on Next after Select next, after checking Click Next, followed by
applying displayed the import settings Finish to complete the
settings importing process.
Refresh the Navigator
window in Workbench to
see the imported file
Importing & Exporting Files
Import Data
– Records from a CSV file can be imported into the result set of the workbench
Export a Result Set
– A result set in the workbench can be exported to common file formats including
CSV, JSON, HTML, and XML
Understanding data using two datasets
a) salesman
b) customer
Discuss the data types of the
given data fields before moving
to next slide
Importing and understanding the data
Create database ‘db’ and import “salesman.csv” and “customer.csv”
Data Description: (SHOW COLUMNS FROM)
• Salesman dataset has four columns ( Fields) as shown
1. salesman_id , 2. name, 3. city and 4. commission
The data types of respective columns are shown in its adjacent column
• salesman_id and commission are continuous valued columns comprising numeric quantity
• name and city are categorical columns comprising text ( character/ string) data
Importing and understanding the data
• Customer dataset has five columns (Fields) as shown
1. customer_id, 2. cust_name, 3. city, 4. grade and 5. salesman_id
The data types of respective columns are shown in its adjacent column
• customer_id, grade and salesman_id are continuous columns comprising numeric data
• cust_name and city are categorical columns comprising text ( character/ string) data
Retrieving filtered data using where clause
The WHERE clause is used to filter record
Step 1: Check if all the records are Step 2: Fetching the records of sales person
imported successfully from “salesman” table who lives in Rome.
Filtering null values
‘where’ clause is used along with ‘is null’
constraint and ‘or’ operator to check for
missing values in salesman table
Question: Fetch the names of salesmen who reside in New York city.
Operators
An operator is a reserved word or a character used in an SQL statement's WHERE clause
to perform operations, such as comparisons and arithmetic operations
= Checks if the values of two operands are equal or not, if yes then condition becomes true. (a = b)
Checks if the values of two operands are equal or not, if values are not equal then condition becomes
!= true. (a != b)
Checks if the values of two operands are equal or not, if values are not equal then condition becomes
<> true. (a <> b)
Checks if the value of left operand is greater than the value of right operand, if yes then condition
> becomes true. (a > b)
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes
< true. (a < b)
Assignment:
Q1. Using the table “Salesman”. Write down a SQL query to display the records of
salesperson with salesman_id = 5002
Q2. Using the table “Salesman”. Write down a SQL query to display the records of
salesperson with salesman_id not equals to 5001
Q3. Using the table “Salesman”. Write down a SQL query to display the records of
salesperson with salesman_id less than 5003
Q4. Using the table “Salesman”. Write down a SQL query to display the records of
salesperson with salesman_id greater than 5002
Q5. Using the table “Salesman”. Write down a SQL query to display the records of
salesperson with salesman_id less than or equals to 5003
Q6. Using the table “Salesman”. Write down a SQL query to display the records of
salesperson with salesman_id greater than or equals to 5003
Once you have written your query – then only you should move to the next slides to
check for the answers
Comparision Operators
Operator Description
Once you have written your query – then only you should move to the next slides to
check for the answers
Logical Operators
Fetch the Records of Salesman having Fetch the Records of Salesman having
id = 5001 with grade as 200 id = 5001 or work on grade as 200
To fetch data where all of the given sub- To fetch data where any of the given sub-
query conditions are true. query condition is true.
BASIC FUNCTIONS IN SQL
The COUNT() function gives the number of rows that matches specified conditions
The AVG() function in SQL returns the average value of a numeric column
The SUM() function in SQL returns the total sum of a numeric column
The MIN() function in SQL returns the smallest value of the selected column from the table
The MAX() function in SQL returns the largest value of the selected column from the table
HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition GROUP BY is used to group rows by one or
GROUP BY column_name(s) more columns
HAVING condition
ORDER BY column_name(s); ORDER BY is used to sort the results
Review most used query
There WHERE clause can also be used with GROUP BY, but WHERE filters out records
before any grouping is done. This is just an example :
If you need to filter the results after the grouping has been done,
e.g., to see only departments whose average income is larger than 1000, you need to use
the HAVING clause:
Above query +
HAVING AVG(income) > 1000; --further filer--
6 steps of SQL select statement process
Step 1: Getting Data (From, Join)
Result of these operations is a Cartesian product of our 2 tables
Step 2: Row Filter (Where)
After getting qualified rows, it is passed on to the Where clause.
This evaluates every row using conditional expressions
Step 3: Grouping (Group by)
It will group rows that have the same values into summary rows
Step 4: Group Filter (Having)
The Having clause consists of a logical predicate, it is processed
after the Group by (it no longer refer to individual rows, only to
groups of rows)
Step 5: Return Expressions (Select)
During this step, the processor evaluates what will be printed as
a result of the query, and if there are some functions to run on
data like Distinct, Max, Sqrt, Date, Lower, etc.
Step 6: Order (Order by) and Paging (Limit / Offset)
The final processing steps of the query deal with presentation
ordering and the ability to limit the size of the result set
Become smart using SQL
Knowing how to Google >>>> knowing all the SQL functions by heart.
Say, you need to isolate the 5th letter in a string, so you can simply Google:
"Nth character from a string SQL''
The fact that you don't need to know all the SQL functions at any given point in time is
incredibly liberating.
So don't worry about memorizing all the functions you have learned...
because they're ALWAYS just a simple Google search away
You will be working on case study in the next session – where it is completely fine if you
google and code – become smart using SQL!
Thank you