SlideShare a Scribd company logo
1 of 58
Database
Database is collection of data in a format that can be easily accessed (Digital)
A software application used to manage our DB is called DBMS (Database Management System)
Types of Databases
Relational
Data stored in tables
Non-relational (NoSQL)
data not stored in tables
** We use SQL to work with relational DBMS
What is SQL?
Structured Query Language
SQL is a programming language used to interact with relational databases.
It is used to perform CRUD operations :
Create
Read
Update
Delete
Database Structure
Database
Table 1
Data
Table 2
Data
What is a table?
Student table
CREATE DATABASE db_name;
DROP DATABASE db_name;
Creating our First Database
Our first SQL Query
Creating our First Table
USE db_name;
CREATE TABLE table_name (
column_name1 datatype constraint,
column_name2 datatype constraint,
column_name2 datatype constraint
);
SQL Datatypes
They define the type of values that can be stored in a column
SQL Datatypes
Signed & Unsigned
TINYINT UNSIGNED (0 to 255)
TINYINT (-128 to 127)
Typesof SQLCommands
DDL (Data Definition Language) : create, alter, rename, truncate & drop
DQL(Data Query Language) : select
DML (Data Manipulation Language) : select, insert, update & delete
DCL (Data Control Language) : grant & revoke permission to users
TCL (Transaction Control Language) : start transaction, commit, rollback etc.
Database related Queries
SHOW DATABASES;
SHOW TABLES;
CREATE DATABASE db_name;
CREATE DATABASE IF NOT EXISTS db_name;
DROP DATABASE db_name;
DROP DATABASE IF EXISTS db_name;
Table related Queries
Create
CREATE TABLE table_name (
column_name1 datatype constraint,
column_name2 datatype constraint,
);
Table related Queries
SELECT * FROM table_name;
Select & ViewALLcolumns
Table related Queries
Insert
INSERT INTO table_name
(colname1, colname2);
VALUES
(col1_v1, col2_v1),
(col1_v2, col2_v2);
Keys
Primary Key
It is a column (or set of columns) in a table that uniquely identifies each row. (a unique id)
There is only 1 PK & it should be NOT null.
Foreign Key
A foreign key is a column (or set of columns) in a table that refers to the primary key in another table.
There can be multiple FKs.
FKs can have duplicate & null values.
Keys
table1 - Student table2 - City
Constraints
UNIQUE
PRIMARY KEY
SQL constraints are used to specify rules for data in a table.
NOT NULL columns cannot have a null value
all values in column are different
makes a column unique & not null but used only for one
Constraints
FOREIGN KEY prevent actions that would destroy links between tables
DEFAULT sets the default value of a column
Constraints
CHECK it can limit the values allowed in a column
Create this sample table Insert this data
Select in Detail
used to select any data from the database
Basic Syntax
SELECT col1, col2 FROM table_name;
To Select ALL
SELECT * FROM table_name;
Where Clause
To define some conditions
SELECT col1, col2 FROM table_name
WHERE conditions;
Arithmetic Operators : +(addition) , -(subtraction), *(multiplication), /(division), %(modulus)
Comparison Operators : = (equal to), != (not equal to), > , >=, <, <=
Logical Operators : AND, OR , NOT, IN, BETWEEN, ALL, LIKE, ANY
Bitwise Operators: & (Bitwise AND), | (Bitwise OR)
Where Clause
UsingOperatorsin WHERE
Operators
AND (to check for both conditionsto be true)
OR (to check for one of the conditions to be true)
Between (selects for a given range)
In (matches any value in the list)
NOT (to negate the given condition)
Operators
Limit Clause
Sets an upper limit on number of (tuples)rows to be returned
SELECT col1, col2 FROM table_name
LIMIT number;
Order By Clause
To sort in ascending (ASC)or descending order (DESC)
SELECT col1, col2 FROM table_name
ORDER BY col_name(s) ASC;
Aggregate Functions
Aggregare functions perform a calculation on a set of values, and return a single value.
COUNT( )
MAX( )
MIN( )
SUM( )
AVG()
Get Average marks
Get Maximum Marks
Group By Clause
Groups rows that have the same values into summary rows.
It collects data from multiple records and groups the result by one or more column.
*Generally we use group by with some aggregation function.
Count number of students in each city
HavingClause
Similar to Where i.e. applies some condition on rows.
Used when we want to apply any condition after grouping.
Count number of students in each city where max marks cross 90.
General Order
SELECT column(s)
FROM table_name
WHERE condition
GROUPBY column(s)
HAVING condition
ORDERBY column(s) ASC;
HavingClause
Similar to Where i.e. applies some condition on rows.
Used when we want to apply any condition after grouping.
Count number of students in each city where max marks cross 90.
Table related Queries
Update (to update existingrows)
UPDATE table_name
SET col1 = val1, col2 = val2
WHERE condition;
Table related Queries
Delete (to delete existing rows)
DELETE FROM table_name
WHERE condition;
Cascading for FK
On Delete Cascade
When we create a foreign key using this option, it deletes the referencing rows in the child table
when the referenced row is deleted in the parent table which has a primary key.
On Update Cascade
When we create a foreign key using UPDATE CASCADE the referencing rows are updated in the child
table when the referenced row is updated in the parent table which has a primary key.
Table related Queries
Alter (to change the schema)
ADD Column
ALTER TABLE table_name
ADD COLUMN column_name datatype constraint;
DROP Column
ALTER TABLE table_name
DROP COLUMN column_name;
RENAME Table
ALTER TABLE table_name
RENAME TO new_table_name;
Table related Queries
MODIFY Column (modify datatype/ constraint)
ALTER TABLE table_name
MODIFY col_name new_datatype new_constraint;
CHANGE Column (rename)
ALTER TABLE table_name
CHANGE COLUMN old_name new_name new_datatype new_constraint;
ADD Column
RENAME Table
DROP Column
MODIFY Column
CHANGE Column (rename)
Table related Queries
Truncate (to delete table's data)
TRUNCATE TABLE table_name ;
Joins in SQL
Join is used to combine rows from two or more tables, based on a related column between them.
Types of Joins
Inner Join Left Join Right Join Full Join
Outer Joins
Inner Join
Returns records that have matching values in both tables
Syntax
SELECT column(s)
FROM tableA
INNER JOIN tableB
ON tableA.col_name = tableB.col_name;
Inner Join
Example
SELECT *
FROM student
INNER JOIN course
ON student.student_id = course.student_id;
student course
Result
Left Join
Returns all records from the left table, and the matched records from
the right table
Syntax
SELECT column(s)
FROM tableA
LEFT JOIN tableB
ON tableA.col_name = tableB.col_name;
Left Join
Example
SELECT *
FROM student as s
LEFT JOIN course as c
ON s.student_id = c.student_id;
student course
Result
Right Join
Returns all records from the right table, and the matched records
from the left table
Syntax
SELECT column(s)
FROM tableA
RIGHT JOIN tableB
ON tableA.col_name = tableB.col_name;
Right Join
Example
SELECT *
FROM student as s
RIGHT JOIN course as c
ON s.student_id = c.student_id;
student course
Result
Full Join
Returns all records when there is a match in either left or right table
Syntax in MySQL
LEFT JOIN
UNION
RIGHT JOIN
Full Join
Example
student
course
Result
Qs: Write SQL commands to display the right exclusive join :
Think & Ans
Right Exclusive Join
Left Exclusive Join
Self Join
It is a regular join but the table is joined with itself.
Syntax
SELECT column(s)
FROM table as a
JOIN table as b
ON a.col_name = b.col_name;
Self Join
Example
Employee
Result
Union
It isused to combine the result-set of two or more SELECTstatements.
GivesUNIQUE records.
Syntax
SELECT column(s) FROM tableA
UNION
SELECT column(s) FROM tableB
To use it :
every SELECT should have same no. of columns
columnsmust have similar data types
columns in every SELECT should be in same order
SQLSub Queries
A Subquery or Inner query or a Nested query is a query within another SQL query.
Query
Sub Query
It involves2 select statements.
Syntax
SELECT column(s)
FROM table_name
WHERE col_name operator
( subquery );
SQL Sub Queries
Example
Get names of all students who scored more than class average.
Step 1. Find the avg of class
Step 2. Find the names of students with marks > avg
SQL Sub Queries
Example
Find the names of all students with even roll numbers.
Step 1. Find the even roll numbers
Step 2. Find the names of students with even roll no
SQL Sub Queries
Example with FROM
Find the max marks from the students of Delhi
Step 1. Find the students of Mumbai
Step 2. Find their max marks using the sublist in step 1
MySQL Views
A view is a virtual table based on the result-set of an SQL statement.
*A view always shows up-to-date data. The
database engine recreates the view, every time a
user queries it.

More Related Content

Similar to DBMS and SQL(structured query language) .pptx

Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETEAbrar ali
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...SakkaravarthiS1
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for BeginnersAbdelhay Shafi
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic ConceptsTony Wong
 
SQL Fundamentals
SQL FundamentalsSQL Fundamentals
SQL FundamentalsBrian Foote
 
Sql smart reference_by_prasad
Sql smart reference_by_prasadSql smart reference_by_prasad
Sql smart reference_by_prasadpaddu123
 
Sql smart reference_by_prasad
Sql smart reference_by_prasadSql smart reference_by_prasad
Sql smart reference_by_prasadpaddu123
 
SQL : Structured Query Language
SQL : Structured Query LanguageSQL : Structured Query Language
SQL : Structured Query LanguageAbhishek Gautam
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functionsVikas Gupta
 
SQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptxSQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptxssuser6bf2d1
 
SQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfSQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfDraguClaudiu
 
STRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESSTRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESVENNILAV6
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Trainingbixxman
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptxEllenGracePorras
 

Similar to DBMS and SQL(structured query language) .pptx (20)

Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETE
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic Concepts
 
SQL Fundamentals
SQL FundamentalsSQL Fundamentals
SQL Fundamentals
 
Sql commands
Sql commandsSql commands
Sql commands
 
Query
QueryQuery
Query
 
MySQL Essential Training
MySQL Essential TrainingMySQL Essential Training
MySQL Essential Training
 
Database Overview
Database OverviewDatabase Overview
Database Overview
 
Sql smart reference_by_prasad
Sql smart reference_by_prasadSql smart reference_by_prasad
Sql smart reference_by_prasad
 
Sql smart reference_by_prasad
Sql smart reference_by_prasadSql smart reference_by_prasad
Sql smart reference_by_prasad
 
SQL : Structured Query Language
SQL : Structured Query LanguageSQL : Structured Query Language
SQL : Structured Query Language
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
SQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptxSQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptx
 
SQL.ppt
SQL.pptSQL.ppt
SQL.ppt
 
SQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfSQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdf
 
STRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESSTRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIES
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
SQL | DML
SQL | DMLSQL | DML
SQL | DML
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
 

Recently uploaded

Data Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health ClassificationData Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health ClassificationBoston Institute of Analytics
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...Pooja Nehwal
 
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...Suhani Kapoor
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...shivangimorya083
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiVIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiSuhani Kapoor
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998YohFuh
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxFurkanTasci3
 

Recently uploaded (20)

Data Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health ClassificationData Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health Classification
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
 
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service AmravatiVIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
VIP Call Girls in Amravati Aarohi 8250192130 Independent Escort Service Amravati
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptx
 

DBMS and SQL(structured query language) .pptx

  • 1. Database Database is collection of data in a format that can be easily accessed (Digital) A software application used to manage our DB is called DBMS (Database Management System)
  • 2. Types of Databases Relational Data stored in tables Non-relational (NoSQL) data not stored in tables ** We use SQL to work with relational DBMS
  • 3. What is SQL? Structured Query Language SQL is a programming language used to interact with relational databases. It is used to perform CRUD operations : Create Read Update Delete
  • 5. What is a table? Student table
  • 6. CREATE DATABASE db_name; DROP DATABASE db_name; Creating our First Database Our first SQL Query
  • 7. Creating our First Table USE db_name; CREATE TABLE table_name ( column_name1 datatype constraint, column_name2 datatype constraint, column_name2 datatype constraint );
  • 8. SQL Datatypes They define the type of values that can be stored in a column
  • 9. SQL Datatypes Signed & Unsigned TINYINT UNSIGNED (0 to 255) TINYINT (-128 to 127)
  • 10. Typesof SQLCommands DDL (Data Definition Language) : create, alter, rename, truncate & drop DQL(Data Query Language) : select DML (Data Manipulation Language) : select, insert, update & delete DCL (Data Control Language) : grant & revoke permission to users TCL (Transaction Control Language) : start transaction, commit, rollback etc.
  • 11. Database related Queries SHOW DATABASES; SHOW TABLES; CREATE DATABASE db_name; CREATE DATABASE IF NOT EXISTS db_name; DROP DATABASE db_name; DROP DATABASE IF EXISTS db_name;
  • 12. Table related Queries Create CREATE TABLE table_name ( column_name1 datatype constraint, column_name2 datatype constraint, );
  • 13. Table related Queries SELECT * FROM table_name; Select & ViewALLcolumns
  • 14. Table related Queries Insert INSERT INTO table_name (colname1, colname2); VALUES (col1_v1, col2_v1), (col1_v2, col2_v2);
  • 15. Keys Primary Key It is a column (or set of columns) in a table that uniquely identifies each row. (a unique id) There is only 1 PK & it should be NOT null. Foreign Key A foreign key is a column (or set of columns) in a table that refers to the primary key in another table. There can be multiple FKs. FKs can have duplicate & null values.
  • 16. Keys table1 - Student table2 - City
  • 17. Constraints UNIQUE PRIMARY KEY SQL constraints are used to specify rules for data in a table. NOT NULL columns cannot have a null value all values in column are different makes a column unique & not null but used only for one
  • 18. Constraints FOREIGN KEY prevent actions that would destroy links between tables DEFAULT sets the default value of a column
  • 19. Constraints CHECK it can limit the values allowed in a column
  • 20. Create this sample table Insert this data
  • 21. Select in Detail used to select any data from the database Basic Syntax SELECT col1, col2 FROM table_name; To Select ALL SELECT * FROM table_name;
  • 22. Where Clause To define some conditions SELECT col1, col2 FROM table_name WHERE conditions;
  • 23. Arithmetic Operators : +(addition) , -(subtraction), *(multiplication), /(division), %(modulus) Comparison Operators : = (equal to), != (not equal to), > , >=, <, <= Logical Operators : AND, OR , NOT, IN, BETWEEN, ALL, LIKE, ANY Bitwise Operators: & (Bitwise AND), | (Bitwise OR) Where Clause UsingOperatorsin WHERE
  • 24. Operators AND (to check for both conditionsto be true) OR (to check for one of the conditions to be true)
  • 25. Between (selects for a given range) In (matches any value in the list) NOT (to negate the given condition) Operators
  • 26. Limit Clause Sets an upper limit on number of (tuples)rows to be returned SELECT col1, col2 FROM table_name LIMIT number;
  • 27. Order By Clause To sort in ascending (ASC)or descending order (DESC) SELECT col1, col2 FROM table_name ORDER BY col_name(s) ASC;
  • 28. Aggregate Functions Aggregare functions perform a calculation on a set of values, and return a single value. COUNT( ) MAX( ) MIN( ) SUM( ) AVG() Get Average marks Get Maximum Marks
  • 29. Group By Clause Groups rows that have the same values into summary rows. It collects data from multiple records and groups the result by one or more column. *Generally we use group by with some aggregation function. Count number of students in each city
  • 30. HavingClause Similar to Where i.e. applies some condition on rows. Used when we want to apply any condition after grouping. Count number of students in each city where max marks cross 90.
  • 31. General Order SELECT column(s) FROM table_name WHERE condition GROUPBY column(s) HAVING condition ORDERBY column(s) ASC;
  • 32. HavingClause Similar to Where i.e. applies some condition on rows. Used when we want to apply any condition after grouping. Count number of students in each city where max marks cross 90.
  • 33. Table related Queries Update (to update existingrows) UPDATE table_name SET col1 = val1, col2 = val2 WHERE condition;
  • 34. Table related Queries Delete (to delete existing rows) DELETE FROM table_name WHERE condition;
  • 35. Cascading for FK On Delete Cascade When we create a foreign key using this option, it deletes the referencing rows in the child table when the referenced row is deleted in the parent table which has a primary key. On Update Cascade When we create a foreign key using UPDATE CASCADE the referencing rows are updated in the child table when the referenced row is updated in the parent table which has a primary key.
  • 36. Table related Queries Alter (to change the schema) ADD Column ALTER TABLE table_name ADD COLUMN column_name datatype constraint; DROP Column ALTER TABLE table_name DROP COLUMN column_name; RENAME Table ALTER TABLE table_name RENAME TO new_table_name;
  • 37. Table related Queries MODIFY Column (modify datatype/ constraint) ALTER TABLE table_name MODIFY col_name new_datatype new_constraint; CHANGE Column (rename) ALTER TABLE table_name CHANGE COLUMN old_name new_name new_datatype new_constraint;
  • 38. ADD Column RENAME Table DROP Column MODIFY Column CHANGE Column (rename)
  • 39. Table related Queries Truncate (to delete table's data) TRUNCATE TABLE table_name ;
  • 40. Joins in SQL Join is used to combine rows from two or more tables, based on a related column between them.
  • 41. Types of Joins Inner Join Left Join Right Join Full Join Outer Joins
  • 42. Inner Join Returns records that have matching values in both tables Syntax SELECT column(s) FROM tableA INNER JOIN tableB ON tableA.col_name = tableB.col_name;
  • 43. Inner Join Example SELECT * FROM student INNER JOIN course ON student.student_id = course.student_id; student course Result
  • 44. Left Join Returns all records from the left table, and the matched records from the right table Syntax SELECT column(s) FROM tableA LEFT JOIN tableB ON tableA.col_name = tableB.col_name;
  • 45. Left Join Example SELECT * FROM student as s LEFT JOIN course as c ON s.student_id = c.student_id; student course Result
  • 46. Right Join Returns all records from the right table, and the matched records from the left table Syntax SELECT column(s) FROM tableA RIGHT JOIN tableB ON tableA.col_name = tableB.col_name;
  • 47. Right Join Example SELECT * FROM student as s RIGHT JOIN course as c ON s.student_id = c.student_id; student course Result
  • 48. Full Join Returns all records when there is a match in either left or right table Syntax in MySQL LEFT JOIN UNION RIGHT JOIN
  • 50. Qs: Write SQL commands to display the right exclusive join : Think & Ans Right Exclusive Join Left Exclusive Join
  • 51. Self Join It is a regular join but the table is joined with itself. Syntax SELECT column(s) FROM table as a JOIN table as b ON a.col_name = b.col_name;
  • 53. Union It isused to combine the result-set of two or more SELECTstatements. GivesUNIQUE records. Syntax SELECT column(s) FROM tableA UNION SELECT column(s) FROM tableB To use it : every SELECT should have same no. of columns columnsmust have similar data types columns in every SELECT should be in same order
  • 54. SQLSub Queries A Subquery or Inner query or a Nested query is a query within another SQL query. Query Sub Query It involves2 select statements. Syntax SELECT column(s) FROM table_name WHERE col_name operator ( subquery );
  • 55. SQL Sub Queries Example Get names of all students who scored more than class average. Step 1. Find the avg of class Step 2. Find the names of students with marks > avg
  • 56. SQL Sub Queries Example Find the names of all students with even roll numbers. Step 1. Find the even roll numbers Step 2. Find the names of students with even roll no
  • 57. SQL Sub Queries Example with FROM Find the max marks from the students of Delhi Step 1. Find the students of Mumbai Step 2. Find their max marks using the sublist in step 1
  • 58. MySQL Views A view is a virtual table based on the result-set of an SQL statement. *A view always shows up-to-date data. The database engine recreates the view, every time a user queries it.