SlideShare a Scribd company logo
DATABASE BEGINNING
Course: INFO 6210 Data Management and Database Design
Instructor: Prof. Chaiyaporn Mutsalklisana
• DDL
• CREATE
• USE
• ALTER
• DROP
• DML
• INSERT
• SELECT
• UPDATE
• DELETE
• Data Manipulating Functions – Functions in SQL Statement
• Summary Functions
• String and Numeric Functions
• Comparison and Cast Functions
• Control Flow Functions
• Date/Time Functions
OVERVIEW PART - I
• Joins
• Inner Join
• Outer Join
– Left Outer Join
– Right Outer Join
– Full Outer Join
• Cross Join
• Self Join
• Operators – Union and Union All
OVERVIEW PART - II
CREATE DATABASE [IF NOT EXISTS] <database name>;
- IF NOT EXISTS checks whether database with the same name exists or not
USE <database name>;
- Defines the context for creating tables or executing any statement for that database
ALTER DATABASE <database name>
- Allows you to change CHARACTER SET and/or COLLATE of database
 DROP DATABASE <database name>;
- Allows you to delete database with all tables in it
Example:
 CREATE DATABASE [IF NOT EXISTS] Customer;
 USE Customer;
 ALTER DATABASE Customer COLLATE latin1_bin;
 DROP DATABASE Customer;
DATABASE: CREATE, USE, ALTER, DROP
 CREATE TABLE table_name (
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
.... );
Example:
 CREATE TABLE Student (
StudentID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255),
Major varchar(255) );
TABLE: CREATE
• NOT NULL - A value in a column can not be NULL
• UNIQUE - Each row for a column must have a unique value
• PRIMARY KEY - Identifies each record uniquely. Its a
combination of a NOT NULL and UNIQUE. Ensures that a column
(or combination of two or more columns) have an unique
identity which helps to find a particular record in a table more
easily and quickly
• FOREIGN KEY - Ensure the referential integrity of the data in one
table to match values in another table
• CHECK - Ensures that the value in a column meets a specific
condition
• DEFAULT - Specifies a default value when specified none for this
column
CONSTRAINTS
 CREATE TABLE Customer(
CustomerID int,
LastName varchar(255),
FirstName varchar(255),
SSN bigint NOT NULL,
Address varchar(255),
City varchar(255) DEFAULT ‘NA’,
UNIQUE (SSN),
CHECK (CustomerID > -1),
PRIMARY KEY (CustomerID) );
DEFINING CONSTRAINTS
 CREATE TABLE ORDERS
(Order_ID integer,
Order_Date date,
Customer_SID integer,
Amount double,
Primary Key (Order_ID),
Foreign Key (Customer_SID)
REFERENCES CUSTOMER(CustomerID));
FOREIGN KEY CONSTRAINT
INSERT INTO table_name
VALUES (value1,value2,value3,...),(value1,value2,value3,...),…;
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3);
INSERT INTO table_name (column1, column2)
VALUES (value1, value2);
Example:
INSERT INTO Customer(CustomerID,LastName,FirstName,Address,City,SSN)
VALUES (234,'Dinoriya','Ashwin','Longwood','NewYork',1234567890);
INSERT INTO Customer
VALUES (123,'Ingle','Tanmay','Cityview','Boston',9876543210);
INSERT INTO Customer(CustomerID, LastName, Address, City)
VALUES (345,'Cardinal','Skagen 21','Norway');
INSERT INTO
 UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
Example
 UPDATE Customer
SET FirstName=‘Ashwinkumar’, Address=‘Cityview’
WHERE CustomerID=123;
UPDATE
 DELETE FROM table_name
WHERE some_column=some_value;
• Example:
 DELETE FROM Customer
WHERE CustomerID=123;
DELETE
 SELECT column_name_1, column_name_2
FROM table_name ;
 SELECT * FROM table_name ;
Example:
 SELECT LastName, FirstName, City
FROM Customer;
 SELECT * FROM Customer;
SELECT
 DROP TABLE IF EXISTS ‘table_name’;
 TRUNCATE TABLE ‘table_name’;
Example:
 DROP TABLE IF EXISTS Customer;
TRUNCATE TABLE Customer;
So, What is the difference between DROP and TRUNCATE ??
DROP & TRUNCATE TABLE
• AVG() Function
• SUM() Function
• MIN() and MAX() Functions
• COUNT() Function
SUMMARY FUNCTIONS
• GREATEST() – It returns greatest value
Ex: SELECT GREATEST(4, 83, 0, 9, -3);  83
• LEAST() – It returns least value
Ex: SELECT LEAST(4, 83, 0, 9, -3);  -3
• ISNULL()- It returns a value of 1 if the expression evaluates
to NULL; otherwise, the function returns a value of 0
Ex: SELECT ISNULL(1*NULL);  1
COMPARISON FUNCTIONS
• CEIL() or CEILING(): SELECT CEILING(9.327);  10
• FLOOR(): SELECT CEILING(9.327);  9
• COT(<number>) : Calculates cotangent of number
• MOD(n1,n2) : Returns the remainder derived by dividing two
numbers(n1/n2)
• PI() : Returns 3.141593.
• POW(<number>, <power>) and POWER(<number>, <power>): Raises
the value of one number to the power of the second number
• ROUND(4.27943, 2)  4.28
• TRUNCATE(4.27943, 2)  4.27
• SQRT(36)  6
NUMERIC FUNCTIONS
CAST(<expression> AS <type>)
Ex: SELECT CAST(20041031 AS DATE);
• The conversion types available to the CAST() function are as follows:
❑ BINARY
❑ CHAR
❑ DATE
❑ DATETIME
❑ SIGNED and UNSIGNED [INTEGER]
❑ TIME
CONVERT(<expression>, <type>)
• The CONVERT() function allows you to convert dates in the same way as the CAST()
function
CAST FUNCTIONS
• IF() :
 IF(<expression1>, <expression2>, <expression3)
If <expression1> evaluates to true, then the function returns <expression2>;
otherwise, the function returns <expression3>
• IFNULL():
 IFNULL(<expression1>, <expression2>)
The function returns <expression1> if it is not NULL; otherwise, it returns
<expression2>
NULLIF():
IFNULL(<expression1>, <expression2>)
The NULLIF() function returns NULL if <expression1> equals <expression2>;
otherwise, it returns <expression1>
CONTROL FLOW FUNCTIONS
CASE():
CASE WHEN <expression> THEN <result>
[{WHEN <expression> THEN <result>}...]
[ELSE <result>]
END
 The WHEN...THEN clause specifies the expression to be evaluated and the results to be returned if that expression
evaluates to true
CASE <expression>
WHEN <value> THEN <result>
[{WHEN <value> THEN <result>}...]
[ELSE <result>]
END
 The main difference in this version of the CASE() function is that the expression is specified after the keyword CASE,
and the WHEN...THEN clauses include the possible values that result from that expression.
CONTROL FLOW CONT’D…
• CURDATE(), CURRENT_DATE(), CURTIME(), CURRENT_TIME(),
CURRENT_TIMESTAMP(), NOW() : Retrieve current date and
time information
• DATE(), MONTH(), MONTHNAME(), and YEAR(): Allows you to
extract specific information from a date or time value.
• DATEDIFF() and TIMEDIFF() : Determines the differences
between dates and times
• DAY(), DAYOFMONTH(), DAYNAME(), DAYOFWEEK(), and
DAYOFYEAR() : Allows you to pull day-related values out of
date or date/time values
• SECOND(), MINUTE(), HOUR(), and TIME(): Extract time parts
from a time or date/time value
DATE/TIME FUNCTIONS
• CHAR_LENGTH(), CHARACTER_LENGTH(): Both returns the
number of characters in the specified string
• LENGTH() : It returns the length of a string, only the length is
measured in bytes, rather than characters.
• CHARSET() and COLLATION() : Returns Character Set and
Collation type of database
• CONCAT() and CONCAT_WS() : allow you to concatenate
data.
• LCASE(), LOWER(), UCASE(), and UPPER() : allow you to
change string values to upper or lowercase
• LEFT(<string>, <length>) and RIGHT(<string>, <length>): The
<length> value determines how many characters are
returned, starting at the left/right end of the string
• REPEAT(<string>, <count>) and REVERSE(<string>)
STRING FUNCTIONS
JOINS
• Joins enable you to retrieve related data from different tables and
display that data in one results set.
• It combine rows from two or more tables, based on a common
field between them.
JOIN
• Now when we look at the data of Product table we can see Vendor and Category in
the form of a foreign key i.e. a number.
• If we want to see a table with three columns namely productname, VendorName and
CategoryName we will have to link the above tables. This link is called as ‘JOIN’.
• There are 6 types of Joins – Inner Join, Left Outer Join, Right Outer Join, Full Join, Self Join
and Cross Join. Lets look at each of them.
JOIN
• The INNER JOIN selects only those rows from both tables
where there is a match between the common column in
both tables.
INNER JOIN
INNER JOIN - EXAMPLE
If we want to create a temporary table, just for
display sake with two columns, first column being
ProductName and second column being
VendorName, we create a Join between the
two tables on the common column which in this
case is VendorID
As you can see only the columns with common VendorIDs are displayed. Since there is no product with
VendorID 9,10 or 11, those Vendor Names are not displayed. This is called as inner join.
• SELECT column 1, column 2, column 3, etc.
FROM table1
JOIN/INNER JOIN table2
ON table1.column_name=table2.column_name;
INNER JOIN - SYNTAX
• Left outer join
• Right outer join
• Full outer join
OUTER JOINS
• Return all rows from the left table, and the matched rows
from the right table
• The result is NULL in the right side when there is no match
• Also called as ‘left join’
LEFT OUTER JOIN
LEFT OUTER JOIN - EXAMPLE
If we want to include all the vendornames
irrespective of if they are present in the
product table we use left outer join
keeping Vendor table on the left
Thus it includes every value present in the left table and displays ‘Null”
value in the other column. Thus we can understand that there is no
product from the vendors like Dell, Panasonic and HP.
LEFT JOIN - SYNTAX
SELECT column 1, column 2, column 3, etc.
FROM table1
LEFT JOIN/LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;
• Return all rows from the right table, and the matched
rows from the left table
• The result is NULL in the right side when there is no match
• Also called as ‘right join’
RIGHT OUTER JOIN
RIGHT OUTER JOIN - EXAMPLE
Right Outer Join works exactly like
left outer join but in the reverse way.
In the above query we have created a right join on Vendor and
product in which product table is on the right.
Since there are no such values in Product which aren’t present in
Vendor, the result displayed is same as inner join result.
RIGHT JOIN - SYNTAX
SELECT column 1, column 2, column 3, etc.
FROM table1
RIGHT JOIN/RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;
• The FULL OUTER JOIN returns all rows from the left table and from
the right table.
• It combines the result of both LEFT and RIGHT joins.
FULL OUTER JOIN
FULL OUTER JOIN EXAMPLE
Full Join displays all rows of the foreign key
column from both the tables irrespective of
if that value is present in the other table.
Can you
guess?
Since there is no such value of foreign key in Product table which isn’t
in Vendor table, this query will give results similar to those of left join on
vendor and product or right join on product and vendor.
FULL OUTER JOIN - SYNTAX
SELECT column 1, column 2, column 3, etc.
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
• Returns the Cartesian product of rows from tables in the join.
• It will produce rows which combine each row from the first table with each row
from the second table
• Within SELECT statement, use CROSS JOIN explicitly or implicitly
CROSS JOIN
CROSS JOIN - EXAMPLE
This is the least used join and most basic
one. It includes all rows from both the tables
in the result set. The result set is the Cartesian
product of both the tables, such that all the
rows from one table combined with all the
rows from another table.
It will contain 11* 7 = 77 rows.
• A self-join occurs when a table is joined to itself rather than to another table
• Self-joins are also very useful in conjunction with subqueries
• When joining a table to itself, you must give the table an alias
• To give a table or column an alias, you simply put the keyword AS after the
table or column name and specify what you want the table to be known as.
SELECT VendorName AS VN
FROM Vendor
SELF JOIN
• If you need to compare the same fields but different records, you need a self-join.
• If you want to know all the students who stay at the same address
SELECT D1.StudentId, D1.Name, D1.Address, D2.StudentId,D2.Name, D2.Address
FROM Details AS D1 INNER JOIN Details AS D2
ON D1.Address = D2.Address AND
D1.StudentId < D2.StudentId;
SELF JOIN - EXAMPLE
StudentId Name Address
1 Tanmay 75 St. Alphonsus Street
2 Ashwin 75 St. Alphonsus Street
3 Shubham 22 parker street
4 Devashri 22 parker street
• At times, you might want to combine the results of two quite distinct queries.
There may be no link between the results of each query; you just want to display
them all in one results set.
• You can join the results from two or more SELECT queries into one results set by
using the UNION operator.
• Each query must produce the same number of columns and columns’ data
types must be the same
OPERATORS – UNION, UNION ALL
SELECT myColumn, myOtherColumn, someColumn FROM MyTable
UNION
SELECT anotherColumn, yetAnotherColumn, MoreColumn FROM MyOtherTable;
SELECT myColumn FROM MyTable
UNION
SELECT anotherColumn, yetAnotherColumn, MoreColumn FROM MyOtherTable;
ASHWIN AND TANMAY
Created By
Thank You !!

More Related Content

What's hot (20)

PPTX
Presentation slides of Sequence Query Language (SQL)
Punjab University
 
PPTX
Sql subquery
Raveena Thakur
 
PPTX
Sql Constraints
I L0V3 CODING DR
 
PPTX
5. stored procedure and functions
Amrit Kaur
 
PPTX
SUBQUERIES.pptx
RenugadeviR5
 
PPTX
Structured query language(sql)ppt
Gowarthini
 
PPT
Sql basics and DDL statements
Mohd Tousif
 
PPTX
Sql commands
Pooja Dixit
 
PPT
Sql – Structured Query Language
pandey3045_bit
 
PPTX
SQL Commands
Sachidananda M H
 
PPT
Advanced Sql Training
bixxman
 
PDF
SQL Functions and Operators
Mohan Kumar.R
 
PDF
Alter table command
ravikhandelwal41
 
PPTX
Database Testing
Siva Kotilingam Pallikonda
 
PDF
SQL JOINS
Swapnali Pawar
 
PPT
Introduction to-sql
BG Java EE Course
 
ODP
Oracle SQL Advanced
Dhananjay Goel
 
PDF
Window functions in MySQL 8.0
Mydbops
 
Presentation slides of Sequence Query Language (SQL)
Punjab University
 
Sql subquery
Raveena Thakur
 
Sql Constraints
I L0V3 CODING DR
 
5. stored procedure and functions
Amrit Kaur
 
SUBQUERIES.pptx
RenugadeviR5
 
Structured query language(sql)ppt
Gowarthini
 
Sql basics and DDL statements
Mohd Tousif
 
Sql commands
Pooja Dixit
 
Sql – Structured Query Language
pandey3045_bit
 
SQL Commands
Sachidananda M H
 
Advanced Sql Training
bixxman
 
SQL Functions and Operators
Mohan Kumar.R
 
Alter table command
ravikhandelwal41
 
Database Testing
Siva Kotilingam Pallikonda
 
SQL JOINS
Swapnali Pawar
 
Introduction to-sql
BG Java EE Course
 
Oracle SQL Advanced
Dhananjay Goel
 
Window functions in MySQL 8.0
Mydbops
 

Viewers also liked (20)

PDF
Database Systems - SQL - DDL Statements (Chapter 3/2)
Vidyasagar Mundroy
 
PDF
Tomasz Kopacz: Architektura i service fabric - jak budować aplikacje w paas v2
AnalyticsConf
 
PDF
Dawid Gonzo Kałędowski: R jako osobisty GPS
AnalyticsConf
 
PPTX
Final_Project
Ashwin Dinoriya
 
PPTX
Tor Hovland: Taking a swim in the big data lake
AnalyticsConf
 
PPTX
Final presentation
Ashwin Dinoriya
 
PDF
What Is Reporting Services?
LearnItFirst.com
 
PPTX
PowerBI - Porto.Data - 20150219
Rui Romano
 
PDF
Denny Lee\'s Data Camp v1.0 talk on SSRS Best Practices for IT
Bala Subra
 
PDF
Banking Database
Ashwin Dinoriya
 
PPTX
OpenRefine Class Tutorial
Ashwin Dinoriya
 
PPTX
Subqueries, Backups, Users and Privileges
Ashwin Dinoriya
 
PPTX
Data Visualization-Ashwin
Ashwin Dinoriya
 
PPTX
Sql Server 2012 Reporting-Services is Now a SharePoint Service Application
InnoTech
 
PPTX
Welcome to PowerBI and Tableau
Ashwin Dinoriya
 
PPTX
Rafał Korszuń: Security in Design of Cloud Applications
AnalyticsConf
 
PPTX
Paweł Ciepły: PowerBI part1
AnalyticsConf
 
PPTX
SQL Server Reporting Services (SSRS) 101
Sparkhound Inc.
 
PPTX
SQL Server Reporting Services
Ahmed Elbaz
 
PPTX
Ssrs introduction session 1
Muthuvel P
 
Database Systems - SQL - DDL Statements (Chapter 3/2)
Vidyasagar Mundroy
 
Tomasz Kopacz: Architektura i service fabric - jak budować aplikacje w paas v2
AnalyticsConf
 
Dawid Gonzo Kałędowski: R jako osobisty GPS
AnalyticsConf
 
Final_Project
Ashwin Dinoriya
 
Tor Hovland: Taking a swim in the big data lake
AnalyticsConf
 
Final presentation
Ashwin Dinoriya
 
What Is Reporting Services?
LearnItFirst.com
 
PowerBI - Porto.Data - 20150219
Rui Romano
 
Denny Lee\'s Data Camp v1.0 talk on SSRS Best Practices for IT
Bala Subra
 
Banking Database
Ashwin Dinoriya
 
OpenRefine Class Tutorial
Ashwin Dinoriya
 
Subqueries, Backups, Users and Privileges
Ashwin Dinoriya
 
Data Visualization-Ashwin
Ashwin Dinoriya
 
Sql Server 2012 Reporting-Services is Now a SharePoint Service Application
InnoTech
 
Welcome to PowerBI and Tableau
Ashwin Dinoriya
 
Rafał Korszuń: Security in Design of Cloud Applications
AnalyticsConf
 
Paweł Ciepły: PowerBI part1
AnalyticsConf
 
SQL Server Reporting Services (SSRS) 101
Sparkhound Inc.
 
SQL Server Reporting Services
Ahmed Elbaz
 
Ssrs introduction session 1
Muthuvel P
 
Ad

Similar to DDL,DML,SQL Functions and Joins (20)

PPTX
SQL Server Learning Drive
TechandMate
 
PPTX
Database Overview
Livares Technologies Pvt Ltd
 
PPT
Mysql 120831075600-phpapp01
sagaroceanic11
 
PPTX
Unit_9.pptx
BhagyasriPatel1
 
PPTX
Aggregate functions in SQL.pptx
SherinRappai1
 
PPT
INTRODUCTION TO SQL QUERIES REALTED BRIEF
VADAPALLYPRAVEENKUMA1
 
DOCX
SQL report
Ahmad Zahid
 
PPTX
Aggregate functions in SQL.pptx
SherinRappai
 
PDF
Sql overview-1232931296681161-1
sagaroceanic11
 
PDF
Dbms
Sachin Yadav
 
PPT
MY SQL
sundar
 
PPT
Db1 lecture4
Sherif Gad
 
PPTX
ADVANCE ITT BY PRASAD
PADYALAMAITHILINATHA
 
PPTX
OracleSQLraining.pptx
Rajendra Jain
 
PPT
Ms sql server ii
Iblesoft
 
PPT
Ch 9 S Q L
guest8fdbdd
 
PDF
Chapter9 more on database and sql
KV(AFS) Utarlai, Barmer (Rajasthan)
 
PPTX
Sql practise for beginners
ISsoft
 
PPT
Review of SQL
Information Technology
 
SQL Server Learning Drive
TechandMate
 
Database Overview
Livares Technologies Pvt Ltd
 
Mysql 120831075600-phpapp01
sagaroceanic11
 
Unit_9.pptx
BhagyasriPatel1
 
Aggregate functions in SQL.pptx
SherinRappai1
 
INTRODUCTION TO SQL QUERIES REALTED BRIEF
VADAPALLYPRAVEENKUMA1
 
SQL report
Ahmad Zahid
 
Aggregate functions in SQL.pptx
SherinRappai
 
Sql overview-1232931296681161-1
sagaroceanic11
 
MY SQL
sundar
 
Db1 lecture4
Sherif Gad
 
ADVANCE ITT BY PRASAD
PADYALAMAITHILINATHA
 
OracleSQLraining.pptx
Rajendra Jain
 
Ms sql server ii
Iblesoft
 
Ch 9 S Q L
guest8fdbdd
 
Chapter9 more on database and sql
KV(AFS) Utarlai, Barmer (Rajasthan)
 
Sql practise for beginners
ISsoft
 
Review of SQL
Information Technology
 
Ad

DDL,DML,SQL Functions and Joins

  • 1. DATABASE BEGINNING Course: INFO 6210 Data Management and Database Design Instructor: Prof. Chaiyaporn Mutsalklisana
  • 2. • DDL • CREATE • USE • ALTER • DROP • DML • INSERT • SELECT • UPDATE • DELETE • Data Manipulating Functions – Functions in SQL Statement • Summary Functions • String and Numeric Functions • Comparison and Cast Functions • Control Flow Functions • Date/Time Functions OVERVIEW PART - I
  • 3. • Joins • Inner Join • Outer Join – Left Outer Join – Right Outer Join – Full Outer Join • Cross Join • Self Join • Operators – Union and Union All OVERVIEW PART - II
  • 4. CREATE DATABASE [IF NOT EXISTS] <database name>; - IF NOT EXISTS checks whether database with the same name exists or not USE <database name>; - Defines the context for creating tables or executing any statement for that database ALTER DATABASE <database name> - Allows you to change CHARACTER SET and/or COLLATE of database  DROP DATABASE <database name>; - Allows you to delete database with all tables in it Example:  CREATE DATABASE [IF NOT EXISTS] Customer;  USE Customer;  ALTER DATABASE Customer COLLATE latin1_bin;  DROP DATABASE Customer; DATABASE: CREATE, USE, ALTER, DROP
  • 5.  CREATE TABLE table_name ( column_name1 data_type(size), column_name2 data_type(size), column_name3 data_type(size), .... ); Example:  CREATE TABLE Student ( StudentID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255), Major varchar(255) ); TABLE: CREATE
  • 6. • NOT NULL - A value in a column can not be NULL • UNIQUE - Each row for a column must have a unique value • PRIMARY KEY - Identifies each record uniquely. Its a combination of a NOT NULL and UNIQUE. Ensures that a column (or combination of two or more columns) have an unique identity which helps to find a particular record in a table more easily and quickly • FOREIGN KEY - Ensure the referential integrity of the data in one table to match values in another table • CHECK - Ensures that the value in a column meets a specific condition • DEFAULT - Specifies a default value when specified none for this column CONSTRAINTS
  • 7.  CREATE TABLE Customer( CustomerID int, LastName varchar(255), FirstName varchar(255), SSN bigint NOT NULL, Address varchar(255), City varchar(255) DEFAULT ‘NA’, UNIQUE (SSN), CHECK (CustomerID > -1), PRIMARY KEY (CustomerID) ); DEFINING CONSTRAINTS
  • 8.  CREATE TABLE ORDERS (Order_ID integer, Order_Date date, Customer_SID integer, Amount double, Primary Key (Order_ID), Foreign Key (Customer_SID) REFERENCES CUSTOMER(CustomerID)); FOREIGN KEY CONSTRAINT
  • 9. INSERT INTO table_name VALUES (value1,value2,value3,...),(value1,value2,value3,...),…; INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3); INSERT INTO table_name (column1, column2) VALUES (value1, value2); Example: INSERT INTO Customer(CustomerID,LastName,FirstName,Address,City,SSN) VALUES (234,'Dinoriya','Ashwin','Longwood','NewYork',1234567890); INSERT INTO Customer VALUES (123,'Ingle','Tanmay','Cityview','Boston',9876543210); INSERT INTO Customer(CustomerID, LastName, Address, City) VALUES (345,'Cardinal','Skagen 21','Norway'); INSERT INTO
  • 10.  UPDATE table_name SET column1=value1,column2=value2,... WHERE some_column=some_value; Example  UPDATE Customer SET FirstName=‘Ashwinkumar’, Address=‘Cityview’ WHERE CustomerID=123; UPDATE
  • 11.  DELETE FROM table_name WHERE some_column=some_value; • Example:  DELETE FROM Customer WHERE CustomerID=123; DELETE
  • 12.  SELECT column_name_1, column_name_2 FROM table_name ;  SELECT * FROM table_name ; Example:  SELECT LastName, FirstName, City FROM Customer;  SELECT * FROM Customer; SELECT
  • 13.  DROP TABLE IF EXISTS ‘table_name’;  TRUNCATE TABLE ‘table_name’; Example:  DROP TABLE IF EXISTS Customer; TRUNCATE TABLE Customer; So, What is the difference between DROP and TRUNCATE ?? DROP & TRUNCATE TABLE
  • 14. • AVG() Function • SUM() Function • MIN() and MAX() Functions • COUNT() Function SUMMARY FUNCTIONS
  • 15. • GREATEST() – It returns greatest value Ex: SELECT GREATEST(4, 83, 0, 9, -3);  83 • LEAST() – It returns least value Ex: SELECT LEAST(4, 83, 0, 9, -3);  -3 • ISNULL()- It returns a value of 1 if the expression evaluates to NULL; otherwise, the function returns a value of 0 Ex: SELECT ISNULL(1*NULL);  1 COMPARISON FUNCTIONS
  • 16. • CEIL() or CEILING(): SELECT CEILING(9.327);  10 • FLOOR(): SELECT CEILING(9.327);  9 • COT(<number>) : Calculates cotangent of number • MOD(n1,n2) : Returns the remainder derived by dividing two numbers(n1/n2) • PI() : Returns 3.141593. • POW(<number>, <power>) and POWER(<number>, <power>): Raises the value of one number to the power of the second number • ROUND(4.27943, 2)  4.28 • TRUNCATE(4.27943, 2)  4.27 • SQRT(36)  6 NUMERIC FUNCTIONS
  • 17. CAST(<expression> AS <type>) Ex: SELECT CAST(20041031 AS DATE); • The conversion types available to the CAST() function are as follows: ❑ BINARY ❑ CHAR ❑ DATE ❑ DATETIME ❑ SIGNED and UNSIGNED [INTEGER] ❑ TIME CONVERT(<expression>, <type>) • The CONVERT() function allows you to convert dates in the same way as the CAST() function CAST FUNCTIONS
  • 18. • IF() :  IF(<expression1>, <expression2>, <expression3) If <expression1> evaluates to true, then the function returns <expression2>; otherwise, the function returns <expression3> • IFNULL():  IFNULL(<expression1>, <expression2>) The function returns <expression1> if it is not NULL; otherwise, it returns <expression2> NULLIF(): IFNULL(<expression1>, <expression2>) The NULLIF() function returns NULL if <expression1> equals <expression2>; otherwise, it returns <expression1> CONTROL FLOW FUNCTIONS
  • 19. CASE(): CASE WHEN <expression> THEN <result> [{WHEN <expression> THEN <result>}...] [ELSE <result>] END  The WHEN...THEN clause specifies the expression to be evaluated and the results to be returned if that expression evaluates to true CASE <expression> WHEN <value> THEN <result> [{WHEN <value> THEN <result>}...] [ELSE <result>] END  The main difference in this version of the CASE() function is that the expression is specified after the keyword CASE, and the WHEN...THEN clauses include the possible values that result from that expression. CONTROL FLOW CONT’D…
  • 20. • CURDATE(), CURRENT_DATE(), CURTIME(), CURRENT_TIME(), CURRENT_TIMESTAMP(), NOW() : Retrieve current date and time information • DATE(), MONTH(), MONTHNAME(), and YEAR(): Allows you to extract specific information from a date or time value. • DATEDIFF() and TIMEDIFF() : Determines the differences between dates and times • DAY(), DAYOFMONTH(), DAYNAME(), DAYOFWEEK(), and DAYOFYEAR() : Allows you to pull day-related values out of date or date/time values • SECOND(), MINUTE(), HOUR(), and TIME(): Extract time parts from a time or date/time value DATE/TIME FUNCTIONS
  • 21. • CHAR_LENGTH(), CHARACTER_LENGTH(): Both returns the number of characters in the specified string • LENGTH() : It returns the length of a string, only the length is measured in bytes, rather than characters. • CHARSET() and COLLATION() : Returns Character Set and Collation type of database • CONCAT() and CONCAT_WS() : allow you to concatenate data. • LCASE(), LOWER(), UCASE(), and UPPER() : allow you to change string values to upper or lowercase • LEFT(<string>, <length>) and RIGHT(<string>, <length>): The <length> value determines how many characters are returned, starting at the left/right end of the string • REPEAT(<string>, <count>) and REVERSE(<string>) STRING FUNCTIONS
  • 22. JOINS
  • 23. • Joins enable you to retrieve related data from different tables and display that data in one results set. • It combine rows from two or more tables, based on a common field between them. JOIN
  • 24. • Now when we look at the data of Product table we can see Vendor and Category in the form of a foreign key i.e. a number. • If we want to see a table with three columns namely productname, VendorName and CategoryName we will have to link the above tables. This link is called as ‘JOIN’. • There are 6 types of Joins – Inner Join, Left Outer Join, Right Outer Join, Full Join, Self Join and Cross Join. Lets look at each of them. JOIN
  • 25. • The INNER JOIN selects only those rows from both tables where there is a match between the common column in both tables. INNER JOIN
  • 26. INNER JOIN - EXAMPLE If we want to create a temporary table, just for display sake with two columns, first column being ProductName and second column being VendorName, we create a Join between the two tables on the common column which in this case is VendorID As you can see only the columns with common VendorIDs are displayed. Since there is no product with VendorID 9,10 or 11, those Vendor Names are not displayed. This is called as inner join.
  • 27. • SELECT column 1, column 2, column 3, etc. FROM table1 JOIN/INNER JOIN table2 ON table1.column_name=table2.column_name; INNER JOIN - SYNTAX
  • 28. • Left outer join • Right outer join • Full outer join OUTER JOINS
  • 29. • Return all rows from the left table, and the matched rows from the right table • The result is NULL in the right side when there is no match • Also called as ‘left join’ LEFT OUTER JOIN
  • 30. LEFT OUTER JOIN - EXAMPLE If we want to include all the vendornames irrespective of if they are present in the product table we use left outer join keeping Vendor table on the left Thus it includes every value present in the left table and displays ‘Null” value in the other column. Thus we can understand that there is no product from the vendors like Dell, Panasonic and HP.
  • 31. LEFT JOIN - SYNTAX SELECT column 1, column 2, column 3, etc. FROM table1 LEFT JOIN/LEFT OUTER JOIN table2 ON table1.column_name=table2.column_name;
  • 32. • Return all rows from the right table, and the matched rows from the left table • The result is NULL in the right side when there is no match • Also called as ‘right join’ RIGHT OUTER JOIN
  • 33. RIGHT OUTER JOIN - EXAMPLE Right Outer Join works exactly like left outer join but in the reverse way. In the above query we have created a right join on Vendor and product in which product table is on the right. Since there are no such values in Product which aren’t present in Vendor, the result displayed is same as inner join result.
  • 34. RIGHT JOIN - SYNTAX SELECT column 1, column 2, column 3, etc. FROM table1 RIGHT JOIN/RIGHT OUTER JOIN table2 ON table1.column_name=table2.column_name;
  • 35. • The FULL OUTER JOIN returns all rows from the left table and from the right table. • It combines the result of both LEFT and RIGHT joins. FULL OUTER JOIN
  • 36. FULL OUTER JOIN EXAMPLE Full Join displays all rows of the foreign key column from both the tables irrespective of if that value is present in the other table. Can you guess? Since there is no such value of foreign key in Product table which isn’t in Vendor table, this query will give results similar to those of left join on vendor and product or right join on product and vendor.
  • 37. FULL OUTER JOIN - SYNTAX SELECT column 1, column 2, column 3, etc. FROM table1 FULL OUTER JOIN table2 ON table1.column_name=table2.column_name;
  • 38. • Returns the Cartesian product of rows from tables in the join. • It will produce rows which combine each row from the first table with each row from the second table • Within SELECT statement, use CROSS JOIN explicitly or implicitly CROSS JOIN
  • 39. CROSS JOIN - EXAMPLE This is the least used join and most basic one. It includes all rows from both the tables in the result set. The result set is the Cartesian product of both the tables, such that all the rows from one table combined with all the rows from another table. It will contain 11* 7 = 77 rows.
  • 40. • A self-join occurs when a table is joined to itself rather than to another table • Self-joins are also very useful in conjunction with subqueries • When joining a table to itself, you must give the table an alias • To give a table or column an alias, you simply put the keyword AS after the table or column name and specify what you want the table to be known as. SELECT VendorName AS VN FROM Vendor SELF JOIN
  • 41. • If you need to compare the same fields but different records, you need a self-join. • If you want to know all the students who stay at the same address SELECT D1.StudentId, D1.Name, D1.Address, D2.StudentId,D2.Name, D2.Address FROM Details AS D1 INNER JOIN Details AS D2 ON D1.Address = D2.Address AND D1.StudentId < D2.StudentId; SELF JOIN - EXAMPLE StudentId Name Address 1 Tanmay 75 St. Alphonsus Street 2 Ashwin 75 St. Alphonsus Street 3 Shubham 22 parker street 4 Devashri 22 parker street
  • 42. • At times, you might want to combine the results of two quite distinct queries. There may be no link between the results of each query; you just want to display them all in one results set. • You can join the results from two or more SELECT queries into one results set by using the UNION operator. • Each query must produce the same number of columns and columns’ data types must be the same OPERATORS – UNION, UNION ALL SELECT myColumn, myOtherColumn, someColumn FROM MyTable UNION SELECT anotherColumn, yetAnotherColumn, MoreColumn FROM MyOtherTable; SELECT myColumn FROM MyTable UNION SELECT anotherColumn, yetAnotherColumn, MoreColumn FROM MyOtherTable;
  • 43. ASHWIN AND TANMAY Created By Thank You !!