0% found this document useful (0 votes)
18 views34 pages

Sql Basics

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
18 views34 pages

Sql Basics

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 34

SQL Basics

Dr. Sanjeev Verma


IMS, University of Lucknow,
Lucknow - 226021
What is SQL?
• SQL stands for Structured Query Language. It is
used for storing and managing data in Relational
Database Management System (RDBMS).
• It is a standard language for Relational Database
System. It enables a user to create, read, update
and delete relational databases and tables.
• All the RDBMS like MySQL, Informix, Oracle, MS
Access and SQL Server use SQL as their standard
database language.
• SQL allows users to query the database in a
number of ways, using English-like statements.
What is Advantages of SQL?
• High speed
• No coding needed
• Well defined standards
• Portability
• Interactive language
• Multiple data view
SQL Commands
• SQL commands are instructions. It is used to
communicate with the database. It is also used to
perform specific tasks, functions, and queries of data.

• SQL can perform various tasks like create a table, add


data to tables, drop the table, modify the table, set
permission for users.
Types of SQL Commands
• There are five types of SQL commands: DDL, DML,
DCL, TCL, and DQL.
Data Definition Language (DDL)
• DDL changes the structure of the table like creating a
table, deleting a table, altering a table, etc.
• All the command of DDL are auto-committed that
means it permanently save all the changes in the
database.
• Here are some commands that come under DDL:
 CREATE
 ALTER
 DROP
 TRUNCATE
Data Definition Language (DDL)- CREATE
CREATE It is used to create a new table in the database.

Syntax:
REATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);

Example:
CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email VARCHA
R2(100), DOB DATE);
Data Definition Language (DDL)- Drop
Drop: It is used to delete both the structure and record
stored in the table.

Syntax:
DROP TABLE ;

Example:
DROP TABLE EMPLOYEE;
Data Definition Language (DDL)- ALTER
ALTER: It is used to alter the structure of the database. This change
could be either to modify the characteristics of an existing
attribute or probably to add a new attribute.

Syntax:
ALTER TABLE table_name ADD column_name COLUMN-definition;
ALTER TABLE MODIFY(COLUMN DEFINITION....);

Example:
ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20));
ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR2(20));
Data Definition Language (DDL)- TRUNCATE
TRUNCATE: It is used to delete all the rows from the table and free
the space containing the table.

Syntax:
TRUNCATE TABLE table_name;

Example:
TRUNCATE TABLE EMPLOYEE;
Data Manipulation Language
• DML commands are used to modify the database. It is
responsible for all form of CHANGES in the database.

• The command of DML is not auto-committed that


means it can't permanently save all the changes in the
database. They can be rollback.

Here are some commands that come under DML:


 INSERT
 UPDATE
 DELETE
Data Manipulation Language - INSERT
INSERT: The INSERT statement is a SQL query. It is used to insert
data into the row of a table.
Syntax:
INSERT INTO TABLE_NAME (col1, col2, col3,.... col N)
VALUES (value1, value2, value3, .... valueN);

OR
INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... valueN);

Example:
INSERT INTO XYZ (Author, Subject) VALUES ("Sonoo", "DBMS");
Data Manipulation Language - UPDATE
Update: This command is used to update or modify the value of a
column in the table.
Syntax:
UPDATE table_name SET [column_name1= value1,...column_n
ameN = valueN] [WHERE CONDITION]

Example:
UPDATE students
SET User_Name = 'Sonoo'
WHERE Student_Id = '3'
Data Control Language
DCL commands are used to GRANT and TAKE BACK
authority from any database user.

Here are some commands that come under DCL:


Grant

Revoke
Data Control Language - Grant
GRANT: It is used to give user access privileges to a database.

Example:
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOT
HER_USER;

REVOKE: It is used to take back permissions from the user.

Example:

REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;


Transaction Control Language
TCL commands can only use with DML commands like
INSERT, DELETE and UPDATE only.
These operations are automatically committed in the
database that's why they cannot be used while creating
tables or dropping them.
Here are some commands that come under TCL:
 COMMIT
 ROLLBACK
 SAVEPOINT
Transaction Control Language - COMMIT
Commit: Commit command is used to save all the transactions to
the database.
Syntex:
COMMIT;
Example:
DELETE FROM CUSTOMERS
WHERE AGE = 25;
COMMIT;
Transaction Control Language - Rollback
Rollback: Rollback command is SAVEPOINT: It is used to roll the
used to undo transactions that transaction back to a certain
have not already been saved point without rolling back the
to the database. entire transaction.
Syntex: Syntex:
ROLLBACK; SAVEPOINT SAVEPOINT_NA
ME;
Example:
DELETE FROM CUSTOMERS
WHERE AGE = 25;
ROLLBACK;
Data Query Language
DQL is used to fetch the data from the database.
It uses only one command:
SELECT
a. SELECT: This is the same as the projection operation of
relational algebra. It is used to select the attribute based on
the condition described by WHERE clause.

Syntax:
SELECT expressions FROM TABLES WHERE conditions;
Example:
SELECT emp_name FROM employee WHERE age > 20;
Example:
SQL> CREATE TABLE EMPLOYEE (
EMP_ID INT NOT NULL,
EMP_NAME VARCHAR (25) NOT NULL,
PHONE_NO INT NOT NULL,
ADDRESS CHAR (30),
PRIMARY KEY (ID)
);
• DESC EMPLOYEE;
• DELETE FROM table_name WHERE condition
• DROP TABLE "table_name";
• SELECT * FROM table_name;
• INSERT INTO TABLE_NAME VALUES (value1, value2, value 3, .... Value N);
• INSERT INTO TABLE_NAME[(col1, col2, col3,.... col N)] VALUES (value1, value2, valu
e 3, .... Value N);
• UPDATE table_name SET column_name = value WHERE condition;
Example:
• UPDATE table_name SET column_name = value1, column_name2 = value
WHERE condition;
• DELETE FROM table_name WHERE some_condition;
SQL Sub Query
A Subquery is a query within another SQL query and embedded
within the WHERE clause.
Important Rule:
• A subquery can be placed in a number of SQL clauses like WHERE clause,
FROM clause, HAVING clause.
• You can use Subquery with SELECT, UPDATE, INSERT, DELETE statements
along with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
• A subquery is a query within another query. The outer query is known as
the main query, and the inner query is known as a subquery.
• Subqueries are on the right side of the comparison operator.
• A subquery is enclosed in parentheses.
• In the Subquery, ORDER BY command cannot be used. But GROUP BY
command can be used to perform the same function as ORDER BY
command.
Subqueries with the Select Statement
SQL subqueries are most frequently used with the Select statement.
Syntax:
SELECT column_name
FROM table_name
WHERE column_name expression operator
( SELECT column_name from table_name WHERE ... );

Example:
SELECT *
FROM EMPLOYEE
WHERE ID IN (SELECT ID
FROM EMPLOYEE
WHERE SALARY > 4500);
Subqueries with the INSERT Statement
• SQL subquery can also be used with the Insert statement. In the insert
statement, data returned from the subquery is used to insert into another
table.
• In the subquery, the selected data can be modified with any of the
character, date functions.
Syntax:
INSERT INTO table_name (column1, column2, column3....)
SELECT * FROM table_name WHERE VALUE OPERATOR

Example:
INSERT INTO EMPLOYEE_BKP
SELECT * FROM EMPLOYEE
WHERE ID IN (SELECT ID
FROM EMPLOYEE);
Subqueries with the UPDATE Statement
The subquery of SQL can be used in conjunction with the Update
statement. When a subquery is used with the Update statement,
then either single or multiple columns in a table can be updated.
Syntax:
UPDATE table SET column_name = new_value WHERE VALUE OPERATOR
(SELECT COLUMN_NAME FROM TABLE_NAME WHERE condition);
Example:
Let's assume we have an EMPLOYEE_BKP table available which is backup of
EMPLOYEE table. The given example updates the SALARY by .25 times in the
EMPLOYEE table for all employee whose AGE is greater than or equal to 29.
UPDATE EMPLOYEE
SET SALARY = SALARY * 0.25
WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
WHERE AGE >= 29);
Subqueries with the DELETE Statement
The subquery of SQL can be used in conjunction with the Delete
statement just like any other statements mentioned above.
Syntax:
DELETE FROM TABLE_NAME WHERE VALUE OPERATOR
(SELECT COLUMN_NAME FROM TABLE_NAME WHERE condition);
Example:
Let's assume we have an EMPLOYEE_BKP table available which is backup of
EMPLOYEE table. The given example deletes the records from the EMPLOYEE
table for all EMPLOYEE whose AGE is greater than or equal to 29.

DELETE FROM EMPLOYEE


WHERE AGE IN (SELECT AGE FROM EMPLOYEE_BKP
WHERE AGE >= 29 );
SQL Clauses
GROUP BY
• SQL GROUP BY statement is used to arrange identical data into
groups.
• The GROUP BY statement is used with the SQL SELECT
statement.
• The GROUP BY statement follows the WHERE clause in a
SELECT statement and precedes the ORDER BY clause.
• The GROUP BY statement is used with aggregation function.
Syntax Example
SELECT column
FROM table_name SELECT COMPANY, COUNT(*)
WHERE conditions FROM PRODUCT_MAST
GROUP BY COMPANY;
GROUP BY column
ORDER BY column
ORDER BY
• The ORDER BY clause sorts the result-set in ascending or
descending order.
• It sorts the records in ascending order by default. DESC
keyword is used to sort the records in descending order.

Example
Syntax SELECT *
SELECT column1, column2
FROM table_name FROM CUSTOMER
WHERE condition ORDER BY NAME;
ORDER BY column1, column2... AS OR
C|DESC; SELECT *
FROM CUSTOMER
ORDER BY NAME DESC;
SQL Aggregate Functions
COUNT FUNCTION
• COUNT function is used to Count the number of rows in a database
table. It can work on both numeric and non-numeric data types.
• COUNT function uses the COUNT(*) that returns the count of all the
rows in a specified table. COUNT(*) considers duplicate and Null.
Syntax
COUNT(*) or COUNT( [ALL|DISTINCT] expression )
Example
 SELECT COUNT(*) FROM PRODUCT_MAST;
 SELECT COUNT(*) FROM PRODUCT_MAST; WHERE RATE>=20;
 SELECT COUNT(DISTINCT COMPANY) FROM PRODUCT_MAST;
 SELECT COMPANY, COUNT(*) FROM PRODUCT_MAST GROUP BY COMPANY;
 SELECT COMPANY, COUNT(*) FROM PRODUCT_MAST GROUP BY COMPANY
HAVING COUNT(*)>2;
SUM FUNCTION
• Sum function is used to calculate the sum of all selected
columns. It works on numeric fields only.
Syntax
SUM() or SUM( [ALL|DISTINCT] expression )
Example
SELECT SUM(COST) FROM PRODUCT_MAST;
SUM() with WHERE
SELECT SUM(COST) FROM PRODUCT_MAST WHERE QTY>3;
SUM() with GROUP BY
SELECT SUM(COST) FROM PRODUCT_MAST WHERE QTY>3
GROUP BY COMPANY;
SUM() with HAVING
SELECT COMPANY, SUM(COST) FROM PRODUCT_MAST GROUP BY COM
PANY HAVING SUM(COST)>=170;
AVG FUNCTION
• The AVG function is used to calculate the average value of the
numeric type. AVG function returns the average of all non-Null
values.

Syntax
AVG() or AVG( [ALL|DISTINCT] expression )
Example
SELECT AVG(COST) FROM PRODUCT_MAST;
MAX FUNCTION
• MAX function is used to find the maximum value of a certain column. This
function determines the largest value of all selected values of a column.
Syntax
MAX() or MAX( [ALL|DISTINCT] expression )
Example
SELECT MAX(RATE) FROM PRODUCT_MAST;

MIN FUNCTION
• MIN function is used to find the minimum value of a certain column. This
function determines the smallest value of all selected values of a column
Syntax
MIN() or MIN( [ALL|DISTINCT] expression )
Example
SELECT MIN(RATE) FROM PRODUCT_MAST;

You might also like