SQLPPT
SQLPPT
• When you are executing an SQL command for any RDBMS, the system
determines the best way to carry out your request and SQL engine figures
out how to interpret the task.
• There are various components included in this process.
• These components are −
• Query Dispatcher
• Optimization Engines
• Classic Query Engine
• SQL Query Engine, etc.
• A classic query engine handles all the non-SQL queries, but a SQL query
engine won't handle logical files.
SQL Process
• CREATE
Creates a new table, a view of a table, or other object in the database.
• ALTER
Modifies an existing database object, such as a table.
• DROP
Deletes an entire table, a view of a table or other objects in the
database.
DML - Data Manipulation Language
• SELECT
Retrieves certain records from one or more tables
• INSERT
Creates a record.
• UPDATE
Modifies records.
• DELETE
Deletes records.
DCL - Data Control Language
• GRANT
Gives a privilege to user.
• REVOKE
• Takes back privileges granted from user.
What is RDBMS?
Tables in SQL
Product
Tuples or rows
What is a NULL value?
• For example, the following SQL query creates a new table called
PERSON
• CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
SQL DEFAULT Constraint
• The CHECK constraint is used to limit the value range that can be
placed in a column.
• If you define a CHECK constraint on a single column it allows only
certain values for this column.
• If you define a CHECK constraint on a table it can limit the values in
certain columns based on values in other columns in the row.
SQL CHECK on CREATE TABLE
• SELECT
Retrieves certain records from one or more tables
• INSERT
Creates a record.
• UPDATE
Modifies records.
• DELETE
Deletes records.
SELECT COMMAND
• The SELECT statement is used to select data from a database.
• The data returned is stored in a result table, called the result-set.
SELECT Syntax
SELECT column1, column2, ...
FROM table_name;
SELECT COMMAND
SELECT <attributes>
FROM <one or more relations>
WHERE <conditions>
Simple SQL Query
Product PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi
SELECT *
FROM Product
WHERE category=‘Gadgets’
Output Schema
Details
• Case insensitive:
• Same: SELECT Select select
• Same: Product product
• Different: ‘Seattle’ ‘seattle’
• Constants:
• ‘abc’ - yes
• “abc” - no
Eliminating Duplicates
Category
SELECT DISTINCT category Gadgets
FROM Product
Photography
Household
Compare to:
Category
Gadgets
SELECT category
Gadgets
FROM Product
Photography
Household
Ordering the Results
Ties are broken by the second attribute on the ORDER BY list, etc.
• The WHERE clause can be combined with AND, OR, and NOT
operators.
• The AND and OR operators are used to filter records based on more
than one condition:
• The AND operator displays a record if all the conditions separated by
AND is TRUE.
• The OR operator displays a record if any of the conditions separated
by OR is TRUE.
• The NOT operator displays a record if the condition(s) is NOT TRUE.
SQL AND, OR and NOT Operators
UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
UPDATE Table
• UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
The SQL DELETE Statement
• DELETE Syntax
• DELETE FROM table_name
WHERE condition;
SQL DELETE Example
• The COUNT() function returns the number of rows that matches a specified criteria.
• The AVG() function returns the average value of a numeric column.
• The SUM() function returns the total sum of a numeric column.
• COUNT() Syntax
• SELECT COUNT(column_name)
FROM table_name
WHERE condition;
• AVG() Syntax
• SELECT AVG(column_name)
FROM table_name
WHERE condition;
• SUM() Syntax
• SELECT SUM(column_name)
FROM table_name
WHERE condition;
SQL COUNT(), AVG() and SUM()
SELECT COUNT(ProductID)
FROM Products;
SELECT AVG(Price)
FROM Products;
SELECT SUM(Quantity)
FROM OrderDetails;
SQL LIKE Operator
• SQL Aliases
• SQL aliases are used to give a table, or a column in a table, a
temporary name.
• Aliases are often used to make column names more readable.
• An alias only exists for the duration of the query.
• Alias Column Syntax
• SELECT column_name AS alias_name
FROM table_name;
SQL Aliases
With Grant option Allows the user to grant privileges to some other users
System Privilages
Create procedure Allows the user to create stored procedures,functions and packages
Grant Command for System privilages
• Syntax-Grant create session,create table,create view,create sequence
to username
• Example-Grant create session,create table,create view,create
sequence to abc;
• Output-Grant succeeded
Grant Command for Object privilages
• Grant insert & select privilages on emp table to ABC
Syntax-Grant insert,select
ON emp
TO abc;
• Grant all privilages on emp table to DEF with grant option
Syntax-Grant All
ON emp
TO abc
With Grant Option;
Revoke Privilages
• The privilages granted to a user can be taken back with revoke command
Syntax-Revoke privilages
ON objectname
From username;
Example-Revoke select
ON emp
from abc
Example-Revoke All
ON emp
from Sunil;