SQL(Structured Query Language) is a 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.
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;
11.
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.