SlideShare a Scribd company logo
5
Most read
11
Most read
14
Most read
Presented By:
Mansi Jain
Sr. Software Consultant
Knoldus Inc
Basics on SQL queries &
Query Optimization
Lack of etiquette and manners is a huge turn off.
KnolX Etiquettes
Punctuality
Join the session 5 minutes prior
to the session start time. We
start on time and conclude on
time!
Feedback
Make sure to submit a
constructive feedback for all
sessions as it is very helpful for
the presenter.
Silent Mode
Keep your mobile devices in
silent mode, feel free to move
out of session in case you need
to attend an urgent call.
Avoid Disturbance
Avoid unwanted chit chat during
the session.
Our Agenda
2
01
Query Processing in SQL
3
02
Important clauses/commands of SQL
4
03
5
04
Joins
6
05
Order Of Executions with Example
7
8
Data Definition Language
1 What is SQL ?
Data Manipulation Language
Query Optimization
What is SQL ?
● SQL(Structured Query Language) is an ANSI
standard language for accessing and manipulating
relational databases.
● It includes database creation, deletion, fetching rows,
modifying rows, etc.
● All the Relational Database Management Systems
(RDMS) like MySQL, MS Access, Oracle, Sybase,
Informix, Postgres and SQL Server use SQL as their
standard database language.
Note : Database is collection of data in the form of tables.
Query Processing in SQL
● Each SQL query is gone
through a processor
where it is translated and
optimized.
● Here, parser is basically
translator.
● DBMS Engine is the
executing engine which
executes the data.
● Then, query result
impacts the physical
database.
Important clauses/commands of SQL
● SELECT
● FROM
● WHERE
● GROUP BY
● HAVING
● ORDER BY
● LIMIT
● AS
● JOIN
● IN
● LIKE
● COUNT (column_name)
● SUM (column_name)
● MAX/MIN (column_name)
● AVG(column_name)
● IS NULL/ IS NOT NULL
● COMMIT
● ROLLBACK
● AND
● OR
● DISTINCT
● ON/ USING
Data Definition Language
● CREATE - This is used to create table, database, index and view.
CREATE DATABASE databasename;
CREATE TABLE table_name (column1 datatype, column2 datatype,..);
CREATE INDEX index_name ON table_name (column1, column2,..);
CREATE VIEW view_name AS SELECT column1, column2, ...
FROM table_name WHERE condition;
● ALTER - This is used to add/remove/rename/update column from table
ALTER TABLE table_name ADD column_name datatype;
ALTER TABLE table_name DROP COLUMN column_name;
ALTER TABLE table_name RENAME COLUMN column_name;
ALTER TABLE table_name DROP INDEX index_name; //MySQL
Note : VIEW - Improve security of database by showing intended data to authorised user.
Data Definition Language continue..
● TRUNCATE - This is used to delete data inside the table, not the table itself.
TRUNCATE TABLE table_name;
● DROP - This is used to delete table, database, index and view
DROP DATABASE databasename;
DROP TABLE table_name;
DROP INDEX table_name.index_name; //SQL server
DROP VIEW [view_name];
Data Manipulation Language
● UPDATE - To modify existing data inside a table.
UPDATE table_name
SET column1 = value1, column2 = value2, … WHERE condition;
UPDATE Students
SET StudentName = 'Alfred’, City= 'Delhi' WHERE ID = 1;
● INSERT - To add single row data in to table.
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
INSERT INTO Students (StudentName, City)
VALUES ('Alfred’, 'Delhi');
Data Manipulation Language
● DELETE - To delete specific rows in a table. Once deleted cannot be
recovered.
DELETE FROM table_name WHERE condition;
DELETE FROM Students WHERE StudentName='Alfred';
● SELECT - To select data from a table.
SELECT column1, column2, ...
FROM table_name;
SELECT DISTINCT Country FROM Students;
Basics on SQL queries
Order Of Execution with example
1. FROM - add tables names and joined them together to get base data.
In this command, we can use AS to rename table for reference with alias & JOIN
to add more table and ON to apply check on based on which tables will be joined.
2. WHERE - optional, to filter on resultant base data set
3. GROUP BY - after data is filter, we group rows of resultant set having same value.
4. HAVING - optional, same as Where clause, restrict rows affected by group by clause.
5. SELECT - final result is returned based on above filter clauses.
6. ORDER BY - sort the selected data, by default way of sorting is ASC, can be DESC.
7. LIMIT - restrict the result upto given no. of rows, it gives row no. from where to start and end.
Example - SELECT cs.first_name, MAX(os.amount) AS OrderAmt
FROM Customers AS cs
JOIN Orders AS os ON cs.customer_id = os.customer_id
WHERE os.order_id <4 ORDER BY os.order_id DESC
GROUP by first_name (if column name is in one table only, no need for reference)
HAVING MAX(Orders.amount) <=400 (optional)
LIMIT 2,4 or LIMIT 2 ;
Query Optimization
Process of selecting the most effective way to carry out a SQL statement.
● Avoid Select Distinct - although handy way to remove duplicates from a query, but data may be
grouped to the point of being inaccurate and Inefficient. To avoid using it, select more fields to create
unique results.
● INNER JOIN V/S WHERE - WHERE works with some DBMS system which consider it as INNER JOIN
but not all. This type of join (where) creates a Cartesian Join, also called a Cartesian Product or CROSS
JOIN. Use INNER JOIN instead of WHERE when two or more tables are joined together to get data.
● Select required fields instead of using select * - Specify columns to avoid extra fetching load on DB.
● Use Limit to sample query results- always fetch limited data to decrease response time of a query.
● Use wildcards at the end of a phrase only- as they search the entire database for matching results.
● Use Indexing: Ensure proper indexing for quick access to the database.
● Avoid using clauses like Initcap, Lower, Upper - as they increase the query response time.
Thank You !
,

More Related Content

PPTX
PostgreSQL Database Slides
DOCX
Mohan Testing
PDF
Sql coding-standard-sqlserver
ODP
Mysql database
PDF
Sql for dbaspresentation
PDF
Database development coding standards
PPTX
SQL OVERVIEW for a new introduced student.pptx
PPTX
Relational Database Language.pptx
PostgreSQL Database Slides
Mohan Testing
Sql coding-standard-sqlserver
Mysql database
Sql for dbaspresentation
Database development coding standards
SQL OVERVIEW for a new introduced student.pptx
Relational Database Language.pptx

Similar to Basics on SQL queries (20)

PDF
PPTX
SQL | DML
PPT
[PHPUGPH] PHP Roadshow - MySQL
PPTX
SQL for interview
PPTX
Lab1 select statement
PDF
Goldilocks and the Three MySQL Queries
PPTX
Database models and DBMS languages
PPTX
lovely
DOCX
COMPUTERS SQL
PPTX
Database COMPLETE
PDF
Introduction to Databases - query optimizations for MySQL
PPT
SQL Server 2008 Performance Enhancements
PPTX
SQL.pptx for the begineers and good know
PDF
CS3481_Database Management Laboratory .pdf
PPTX
Sql killedserver
PPTX
My SQL Skills Killed the Server
PPTX
SQL: Structured Query Language
PPT
Module02
SQL | DML
[PHPUGPH] PHP Roadshow - MySQL
SQL for interview
Lab1 select statement
Goldilocks and the Three MySQL Queries
Database models and DBMS languages
lovely
COMPUTERS SQL
Database COMPLETE
Introduction to Databases - query optimizations for MySQL
SQL Server 2008 Performance Enhancements
SQL.pptx for the begineers and good know
CS3481_Database Management Laboratory .pdf
Sql killedserver
My SQL Skills Killed the Server
SQL: Structured Query Language
Module02
Ad

More from Knoldus Inc. (20)

PPTX
Angular Hydration Presentation (FrontEnd)
PPTX
Optimizing Test Execution: Heuristic Algorithm for Self-Healing
PPTX
Self-Healing Test Automation Framework - Healenium
PPTX
Kanban Metrics Presentation (Project Management)
PPTX
Java 17 features and implementation.pptx
PPTX
Chaos Mesh Introducing Chaos in Kubernetes
PPTX
GraalVM - A Step Ahead of JVM Presentation
PPTX
Nomad by HashiCorp Presentation (DevOps)
PPTX
Nomad by HashiCorp Presentation (DevOps)
PPTX
DAPR - Distributed Application Runtime Presentation
PPTX
Introduction to Azure Virtual WAN Presentation
PPTX
Introduction to Argo Rollouts Presentation
PPTX
Intro to Azure Container App Presentation
PPTX
Insights Unveiled Test Reporting and Observability Excellence
PPTX
Introduction to Splunk Presentation (DevOps)
PPTX
Code Camp - Data Profiling and Quality Analysis Framework
PPTX
AWS: Messaging Services in AWS Presentation
PPTX
Amazon Cognito: A Primer on Authentication and Authorization
PPTX
ZIO Http A Functional Approach to Scalable and Type-Safe Web Development
PPTX
Managing State & HTTP Requests In Ionic.
Angular Hydration Presentation (FrontEnd)
Optimizing Test Execution: Heuristic Algorithm for Self-Healing
Self-Healing Test Automation Framework - Healenium
Kanban Metrics Presentation (Project Management)
Java 17 features and implementation.pptx
Chaos Mesh Introducing Chaos in Kubernetes
GraalVM - A Step Ahead of JVM Presentation
Nomad by HashiCorp Presentation (DevOps)
Nomad by HashiCorp Presentation (DevOps)
DAPR - Distributed Application Runtime Presentation
Introduction to Azure Virtual WAN Presentation
Introduction to Argo Rollouts Presentation
Intro to Azure Container App Presentation
Insights Unveiled Test Reporting and Observability Excellence
Introduction to Splunk Presentation (DevOps)
Code Camp - Data Profiling and Quality Analysis Framework
AWS: Messaging Services in AWS Presentation
Amazon Cognito: A Primer on Authentication and Authorization
ZIO Http A Functional Approach to Scalable and Type-Safe Web Development
Managing State & HTTP Requests In Ionic.
Ad

Recently uploaded (20)

PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Cloud computing and distributed systems.
PDF
Empathic Computing: Creating Shared Understanding
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
KodekX | Application Modernization Development
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Electronic commerce courselecture one. Pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Big Data Technologies - Introduction.pptx
Programs and apps: productivity, graphics, security and other tools
Cloud computing and distributed systems.
Empathic Computing: Creating Shared Understanding
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The AUB Centre for AI in Media Proposal.docx
KodekX | Application Modernization Development
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
“AI and Expert System Decision Support & Business Intelligence Systems”
sap open course for s4hana steps from ECC to s4
Electronic commerce courselecture one. Pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
MYSQL Presentation for SQL database connectivity
Spectral efficient network and resource selection model in 5G networks
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Spectroscopy.pptx food analysis technology
Big Data Technologies - Introduction.pptx

Basics on SQL queries

  • 1. Presented By: Mansi Jain Sr. Software Consultant Knoldus Inc Basics on SQL queries & Query Optimization
  • 2. Lack of etiquette and manners is a huge turn off. KnolX Etiquettes Punctuality Join the session 5 minutes prior to the session start time. We start on time and conclude on time! Feedback Make sure to submit a constructive feedback for all sessions as it is very helpful for the presenter. Silent Mode Keep your mobile devices in silent mode, feel free to move out of session in case you need to attend an urgent call. Avoid Disturbance Avoid unwanted chit chat during the session.
  • 3. Our Agenda 2 01 Query Processing in SQL 3 02 Important clauses/commands of SQL 4 03 5 04 Joins 6 05 Order Of Executions with Example 7 8 Data Definition Language 1 What is SQL ? Data Manipulation Language Query Optimization
  • 4. What is SQL ? ● SQL(Structured Query Language) is an ANSI standard language for accessing and manipulating relational databases. ● It includes database creation, deletion, fetching rows, modifying rows, etc. ● All the Relational Database Management Systems (RDMS) like MySQL, MS Access, Oracle, Sybase, Informix, Postgres and SQL Server use SQL as their standard database language. Note : Database is collection of data in the form of tables.
  • 5. Query Processing in SQL ● Each SQL query is gone through a processor where it is translated and optimized. ● Here, parser is basically translator. ● DBMS Engine is the executing engine which executes the data. ● Then, query result impacts the physical database.
  • 6. Important clauses/commands of SQL ● SELECT ● FROM ● WHERE ● GROUP BY ● HAVING ● ORDER BY ● LIMIT ● AS ● JOIN ● IN ● LIKE ● COUNT (column_name) ● SUM (column_name) ● MAX/MIN (column_name) ● AVG(column_name) ● IS NULL/ IS NOT NULL ● COMMIT ● ROLLBACK ● AND ● OR ● DISTINCT ● ON/ USING
  • 7. Data Definition Language ● CREATE - This is used to create table, database, index and view. CREATE DATABASE databasename; CREATE TABLE table_name (column1 datatype, column2 datatype,..); CREATE INDEX index_name ON table_name (column1, column2,..); CREATE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE condition; ● ALTER - This is used to add/remove/rename/update column from table ALTER TABLE table_name ADD column_name datatype; ALTER TABLE table_name DROP COLUMN column_name; ALTER TABLE table_name RENAME COLUMN column_name; ALTER TABLE table_name DROP INDEX index_name; //MySQL Note : VIEW - Improve security of database by showing intended data to authorised user.
  • 8. Data Definition Language continue.. ● TRUNCATE - This is used to delete data inside the table, not the table itself. TRUNCATE TABLE table_name; ● DROP - This is used to delete table, database, index and view DROP DATABASE databasename; DROP TABLE table_name; DROP INDEX table_name.index_name; //SQL server DROP VIEW [view_name];
  • 9. Data Manipulation Language ● UPDATE - To modify existing data inside a table. UPDATE table_name SET column1 = value1, column2 = value2, … WHERE condition; UPDATE Students SET StudentName = 'Alfred’, City= 'Delhi' WHERE ID = 1; ● INSERT - To add single row data in to table. INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); INSERT INTO Students (StudentName, City) VALUES ('Alfred’, 'Delhi');
  • 10. Data Manipulation Language ● DELETE - To delete specific rows in a table. Once deleted cannot be recovered. DELETE FROM table_name WHERE condition; DELETE FROM Students WHERE StudentName='Alfred'; ● SELECT - To select data from a table. SELECT column1, column2, ... FROM table_name; SELECT DISTINCT Country FROM Students;
  • 12. Order Of Execution with example 1. FROM - add tables names and joined them together to get base data. In this command, we can use AS to rename table for reference with alias & JOIN to add more table and ON to apply check on based on which tables will be joined. 2. WHERE - optional, to filter on resultant base data set 3. GROUP BY - after data is filter, we group rows of resultant set having same value. 4. HAVING - optional, same as Where clause, restrict rows affected by group by clause. 5. SELECT - final result is returned based on above filter clauses. 6. ORDER BY - sort the selected data, by default way of sorting is ASC, can be DESC. 7. LIMIT - restrict the result upto given no. of rows, it gives row no. from where to start and end. Example - SELECT cs.first_name, MAX(os.amount) AS OrderAmt FROM Customers AS cs JOIN Orders AS os ON cs.customer_id = os.customer_id WHERE os.order_id <4 ORDER BY os.order_id DESC GROUP by first_name (if column name is in one table only, no need for reference) HAVING MAX(Orders.amount) <=400 (optional) LIMIT 2,4 or LIMIT 2 ;
  • 13. Query Optimization Process of selecting the most effective way to carry out a SQL statement. ● Avoid Select Distinct - although handy way to remove duplicates from a query, but data may be grouped to the point of being inaccurate and Inefficient. To avoid using it, select more fields to create unique results. ● INNER JOIN V/S WHERE - WHERE works with some DBMS system which consider it as INNER JOIN but not all. This type of join (where) creates a Cartesian Join, also called a Cartesian Product or CROSS JOIN. Use INNER JOIN instead of WHERE when two or more tables are joined together to get data. ● Select required fields instead of using select * - Specify columns to avoid extra fetching load on DB. ● Use Limit to sample query results- always fetch limited data to decrease response time of a query. ● Use wildcards at the end of a phrase only- as they search the entire database for matching results. ● Use Indexing: Ensure proper indexing for quick access to the database. ● Avoid using clauses like Initcap, Lower, Upper - as they increase the query response time.