SlideShare a Scribd company logo
SQL
Ch-2: Structure Query Language
Introduction
•What is SQL?
•Need for SQL
•How to create tables in SQL?
•How to add information to tables?
•SELECT … FROM…WHERE (with aggregate functions)
•GROUP BY ….HAVING
•ORDER BY
•UPDATE AND DELETE Command
•ALTER TABLE AND DROP TABLE Command
•EQUI JOIN
What is SQL?
•SQL (Structured Query Language) is a standard language for
accessing and manipulating databases.
•SQL commands are used to create, transform and retrieve information
from Relational Database Management Systems
•also used to create interface between user and database.
•Need for SQL
• SQL commands are used to implement the following;
•SQL can retrieve ,insert ,update ,delete records
•SQL can create new databases
•SQL can create new tables in a database
•SQL can create views in a database
Using Database
•Create data base :
•Create database <database name>;
• >create database school ;
•Following command is used to use a Database mysql>
USE <database name >;
For ex -
mysql> USE school;
A message will come saying- “database changed”
See the Commands
carefully
CREATE TABLE Command
•CREATE TABLE command is used to create table structure.
•we need to give full information about table such as
number of columns,
type of each column and
constraints (primary key)(Optional)
•The CREATE TABLE command requires:
1)Name of the table,
2) Names of fields,
3)Definitions and constrains for each field(optional)
In SQL, we have the following constraints:
NOT NULL - To check a column cannot store NULL value.
PRIMARY KEY - To check that a column have an unique identity
which helps to find a particular record in a table.
Syntax:
CREATE TABLE <table name>
(<column name1> <data type>[size][constraints],
.
.
<column name n> <data type>[size][constraints]);
Table name : Student
Its about a sql topic for basic structured query language
>CREATE TABLE student
(Adno Numeric (3) primary key,
Name varchar (20) not null,
Class Numeric (2),
Section char (1),
Average Numeric (5)
);
If you want view the structure of the table
Use DESC / DESCRIBE command;
DESC STUDENT ; // DESCRIBE STUDENT;
INSERT INTO Command:
This command is used to add rows in the table,
but can add only one row at a time.
Syntax:
INSERT INTO <table name>
[Column_name1, Column_name2, ......Column_name n]
VALUES (value1,value2,value3,….,value n);
OR
INSERT INTO <table name>
VALUES (value1,value2,value3,….,value n);
Note: [] Option
INSERT INTO student VALUES (111,"Anu Jain", 12,"A", 2500);
Note: If we want to insert values to he selective columns then we have to use this
method
INSERT INTO student (ADNO, Name, CLASS) VALUES (777,' LEENA', 'B');
Its about a sql topic for basic structured query language
Its about a sql topic for basic structured query language
Its about a sql topic for basic structured query language
SELECT Command
This command is used to view table information from SQL database.
By using SELECT command, we can get one or more fields information,
while using *, one can get all fields information.
Syntax:
SELECT (* or field list)
FROM <table name>
[WHERE <condition>];
We can specify any condition using where clause.
Where clause is optional.
Example:
1. Display student table information.
SELECT *
FROM student;
2. To display name and class of student table information.
>SELECT name, class FROM student;
3. To display name of 10th class student information.
SELECT name
FROM student
WHERE class = 10;
Operators used in SQL commands:
Arithmetic operators:
Arithmetic operator takes two operands and performs a mathematical
calculation on them.
They can be used only in SELECT command.
The arithmetic operators used in SQL are:
+ Addition - Subtraction * Multiplication / Division
Its about a sql topic for basic structured query language
Its about a sql topic for basic structured query language
Relational operators:
Relational operators are used to implement comparison
between two operands.
These operators can be used only in 'where clause'.
Relational operators are -
< less than
> greater than
< = less than or equal to
> = greater than or equal to
= equal to
! = not equal to
1. Display students' name, who are paying below 3000 fees.
SELECT name FROM student
WHERE fees<3000;
Output:
Name
Anu Jain
Ajit Kumar
Rohan Sharma
2. Display students' name, who are paying above or equal to 3000 fees.
SELECT name FROM student
WHERE fees>=3000;
Output:
Name
Mohit Sharma
Nandini
3. Display students' information, who are not in class 10
SELECT *
FROM student
WHERE class! = 10;
Logical operators:
Logical operators are also possible only in 'where clause' and
are used to merge more than one condition.
Logical operators are:
AND
OR
NOT
Its about a sql topic for basic structured query language
Its about a sql topic for basic structured query language
LIKE OPERATOR: (Wild card Operator)
LIKE OPERATOR is used to search a value similar to specific pattern in a column
using wildcard operator.
There are two wildcard operators - percentage sign (%) and underscore ( _ ).
The percentage sign represents zero, one, or multiple characters (numbers),
while the underscore represents a single number or character.
The symbols can be used in combinations.
Its about a sql topic for basic structured query language
IN Operator :
The IN operator allows us to specify multiple values in a WHERE clause .
For example:
Display students' information, who are in section A and B.
SELECT * FROM student
WHERE section IN ("A","B");
The BETWEEN operator :
The BETWEEN operator is used to test whether or not a value is "between" the two values
stated after the keyword BETWEEN.
For example:
Display students' information, who are paying fees between 2500 and 3500.
SELECT *
FROM student
WHERE fees BETWEEN 2500 AND 3500;
[Note: In the above Query 2500 and 3500 is also included]
ORDER BY command:
This command is used to arrange values in ascending or descending order.
SELECT *
FROM student
ORDER BY fees ASC;
'asc' for ascending order & ‘desc’ for descending order .
Without asc also the list is displayed with ascending order only.
Its about a sql topic for basic structured query language
Aggregate functions
Aggregate functions are used to implement calculation based upon a particular column.
These functions always return a single value.
Aggregate functions are:
1. SUM()
2. AVG()
3. MAX()
4. MIN()
5. COUNT()
Its about a sql topic for basic structured query language
Its about a sql topic for basic structured query language
GROUP BY
The SQL GROUP BY is a clause that enables SQL aggregate functions for grouping of information.
(ie.GROUP BY clause is used in collaboration with the SELECT statement to arrange identical data
into groups.).
This clause is used whenever aggregate functions by group are required.
For example:
1. Display number of students in each class.
SELECT count (*), class
FROM student
GROUP BY class;
2. Display sum of fees for each class.
SELECT class, sum (fees)
FROM student
GROUP BY class;
Having class:
'where' clause is used only to place condition on the selected columns,
'HAVING' clause is used to place condition on groups created by 'group by' clause, because
here the 'WHERE' clause is not useable.
Example:
Display sum of fees which is more than 5000 for each class
SELECT class, sum (fees)
FROM student
GROUP BY class
HAVING sum (fees)>5000;
DISTINCT :
The DISTINCT keyword is used to remove duplicate values in a particular column.
For example:
Display classes in student table.
SELECT class FROM student;
UPDATE Command :
This command is used to implement modification of the data values.
Syntax:
UPDATE <table name>
SET <column name1>=new value, <column name>=new value etc
[WHERE <condition>];
Example:
1. Increase fees value by 500.
UPDATE student
SET fees = fees + 500;
2. Select * from student ;
Increase the fees value by 100 for adno 222.
UPDATE student
SET fees = fees+100
WHERE adno = 222;
DELETE Command:
This command is used to remove information from a particular row or rows.
Note :
This command will delete only row information but not the structure of the table.
Syntax:
DELETE
FROM <table name>
[WHERE <condition>];
For example:
1. Remove adno 444 information.
DELETE
FROM student
WHERE adno = 444;
2. Select * from student;
2. Remove all records.
DELETE
FROM student;
3. Desc student;
ALTER TABLE command
This command is used to implement modification of the structure of the table.
This is a DDL command.
Using this command, we can add a new column, remove the existing column and
modify data type of existing column.
Syntax:
ALTER TABLE <table name>
[ADD/MODIFY/DROP] <column name>;
For example:
1. Add one new column totalfees with number (10, 2).
ALTER TABLE student
ADD totalfees number(10,2);
2. Change totalfees datatype as number(12,2).
ALTER TABLE student
MODIFY totalfees number(12,2);
3. Remove totalfees column.
ALTER TABLE student DROP totalfees;
DROP Command:
This command is used to remove the entire structure of the table and information.
This is also from the DDL command.
Syntax:
DROP TABLE <table name>;
For example:
Remove the whole structure of student table.
DROP TABLE student;
Equi Join
Equi Joins are used to give information in different tables. It is a special
type of join in which we use only equality operator.
For example
SELECT *
FROM product, customer
WHERE product.product_no = customer. procuct_no;
(or)
SELECT *
FROM product p, customer c
WHERE p.product_no=c.procuct_no;
.
PRODUCT CUSTOMER
Its about a sql topic for basic structured query language
NON EQUI JOIN
The SQL NON EQUI JOIN uses comparison operator instead of the equal sign like >, <,
>=, <= along with conditions.
SYNTAX :
SELECT * FROM table_name1, table_name2
WHERE table_name1.column [> | < |!= | >= | <= ] table_name2.column
>Select * from product p, customer c
where p.pdroduct_no != c.product_no;
PRODUCT CUSTOMER
Its about a sql topic for basic structured query language

More Related Content

Similar to Its about a sql topic for basic structured query language (20)

DOC
Introduction sql
sagarasuri
 
PPT
MY SQL
sundar
 
PPT
Db1 lecture4
Sherif Gad
 
PPT
Database queries
laiba29012
 
PPT
Mysql 120831075600-phpapp01
sagaroceanic11
 
DOCX
SQL report
Ahmad Zahid
 
PPT
SQL : introduction
Shakila Mahjabin
 
PDF
Structure query language, database course
yunussufyan2024
 
PPT
SQL.ppt
Ranjit273515
 
PPTX
SQL PPT.pptx
PriyaPandey767008
 
PPTX
Database Akjljljlkjlkjkljlkjldiministration.pptx
EliasPetros
 
PPTX
SQl data base management and design
franckelsania20
 
PPTX
DBMS - Presentationhgfhfhfghfghfhfgh.pptx
SajidHossainKhan1
 
PPTX
Structure Query Language Advance Training
parisaxena1418
 
RTF
Best sql plsql_material for B.TECH
AmIt Prasad
 
DOC
Oracle
Rajeev Uppala
 
PPTX
SQL Presentation & explaination short I.pptx
lankanking4
 
PPTX
SQL Query
Imam340267
 
PPTX
Data Manipulation Language
Jas Singh Bhasin
 
PPT
UNIT2.ppt
SaurabhLokare1
 
Introduction sql
sagarasuri
 
MY SQL
sundar
 
Db1 lecture4
Sherif Gad
 
Database queries
laiba29012
 
Mysql 120831075600-phpapp01
sagaroceanic11
 
SQL report
Ahmad Zahid
 
SQL : introduction
Shakila Mahjabin
 
Structure query language, database course
yunussufyan2024
 
SQL.ppt
Ranjit273515
 
SQL PPT.pptx
PriyaPandey767008
 
Database Akjljljlkjlkjkljlkjldiministration.pptx
EliasPetros
 
SQl data base management and design
franckelsania20
 
DBMS - Presentationhgfhfhfghfghfhfgh.pptx
SajidHossainKhan1
 
Structure Query Language Advance Training
parisaxena1418
 
Best sql plsql_material for B.TECH
AmIt Prasad
 
SQL Presentation & explaination short I.pptx
lankanking4
 
SQL Query
Imam340267
 
Data Manipulation Language
Jas Singh Bhasin
 
UNIT2.ppt
SaurabhLokare1
 

Recently uploaded (20)

PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PPTX
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PPTX
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
epi editorial commitee meeting presentation
MIPLM
 
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
Ad

Its about a sql topic for basic structured query language

  • 1. SQL Ch-2: Structure Query Language Introduction •What is SQL? •Need for SQL •How to create tables in SQL? •How to add information to tables? •SELECT … FROM…WHERE (with aggregate functions) •GROUP BY ….HAVING •ORDER BY •UPDATE AND DELETE Command •ALTER TABLE AND DROP TABLE Command •EQUI JOIN
  • 2. What is SQL? •SQL (Structured Query Language) is a standard language for accessing and manipulating databases. •SQL commands are used to create, transform and retrieve information from Relational Database Management Systems •also used to create interface between user and database. •Need for SQL • SQL commands are used to implement the following; •SQL can retrieve ,insert ,update ,delete records •SQL can create new databases •SQL can create new tables in a database •SQL can create views in a database
  • 3. Using Database •Create data base : •Create database <database name>; • >create database school ; •Following command is used to use a Database mysql> USE <database name >; For ex - mysql> USE school; A message will come saying- “database changed” See the Commands carefully
  • 4. CREATE TABLE Command •CREATE TABLE command is used to create table structure. •we need to give full information about table such as number of columns, type of each column and constraints (primary key)(Optional) •The CREATE TABLE command requires: 1)Name of the table, 2) Names of fields, 3)Definitions and constrains for each field(optional) In SQL, we have the following constraints: NOT NULL - To check a column cannot store NULL value. PRIMARY KEY - To check that a column have an unique identity which helps to find a particular record in a table.
  • 5. Syntax: CREATE TABLE <table name> (<column name1> <data type>[size][constraints], . . <column name n> <data type>[size][constraints]); Table name : Student
  • 7. >CREATE TABLE student (Adno Numeric (3) primary key, Name varchar (20) not null, Class Numeric (2), Section char (1), Average Numeric (5) ); If you want view the structure of the table Use DESC / DESCRIBE command; DESC STUDENT ; // DESCRIBE STUDENT;
  • 8. INSERT INTO Command: This command is used to add rows in the table, but can add only one row at a time. Syntax: INSERT INTO <table name> [Column_name1, Column_name2, ......Column_name n] VALUES (value1,value2,value3,….,value n); OR INSERT INTO <table name> VALUES (value1,value2,value3,….,value n); Note: [] Option INSERT INTO student VALUES (111,"Anu Jain", 12,"A", 2500); Note: If we want to insert values to he selective columns then we have to use this method INSERT INTO student (ADNO, Name, CLASS) VALUES (777,' LEENA', 'B');
  • 12. SELECT Command This command is used to view table information from SQL database. By using SELECT command, we can get one or more fields information, while using *, one can get all fields information. Syntax: SELECT (* or field list) FROM <table name> [WHERE <condition>]; We can specify any condition using where clause. Where clause is optional. Example: 1. Display student table information. SELECT * FROM student;
  • 13. 2. To display name and class of student table information. >SELECT name, class FROM student; 3. To display name of 10th class student information. SELECT name FROM student WHERE class = 10; Operators used in SQL commands: Arithmetic operators: Arithmetic operator takes two operands and performs a mathematical calculation on them. They can be used only in SELECT command. The arithmetic operators used in SQL are: + Addition - Subtraction * Multiplication / Division
  • 16. Relational operators: Relational operators are used to implement comparison between two operands. These operators can be used only in 'where clause'. Relational operators are - < less than > greater than < = less than or equal to > = greater than or equal to = equal to ! = not equal to
  • 17. 1. Display students' name, who are paying below 3000 fees. SELECT name FROM student WHERE fees<3000; Output: Name Anu Jain Ajit Kumar Rohan Sharma 2. Display students' name, who are paying above or equal to 3000 fees. SELECT name FROM student WHERE fees>=3000; Output: Name Mohit Sharma Nandini
  • 18. 3. Display students' information, who are not in class 10 SELECT * FROM student WHERE class! = 10;
  • 19. Logical operators: Logical operators are also possible only in 'where clause' and are used to merge more than one condition. Logical operators are: AND OR NOT
  • 22. LIKE OPERATOR: (Wild card Operator) LIKE OPERATOR is used to search a value similar to specific pattern in a column using wildcard operator. There are two wildcard operators - percentage sign (%) and underscore ( _ ). The percentage sign represents zero, one, or multiple characters (numbers), while the underscore represents a single number or character. The symbols can be used in combinations.
  • 24. IN Operator : The IN operator allows us to specify multiple values in a WHERE clause . For example: Display students' information, who are in section A and B. SELECT * FROM student WHERE section IN ("A","B");
  • 25. The BETWEEN operator : The BETWEEN operator is used to test whether or not a value is "between" the two values stated after the keyword BETWEEN. For example: Display students' information, who are paying fees between 2500 and 3500. SELECT * FROM student WHERE fees BETWEEN 2500 AND 3500; [Note: In the above Query 2500 and 3500 is also included]
  • 26. ORDER BY command: This command is used to arrange values in ascending or descending order. SELECT * FROM student ORDER BY fees ASC; 'asc' for ascending order & ‘desc’ for descending order . Without asc also the list is displayed with ascending order only.
  • 28. Aggregate functions Aggregate functions are used to implement calculation based upon a particular column. These functions always return a single value. Aggregate functions are: 1. SUM() 2. AVG() 3. MAX() 4. MIN() 5. COUNT()
  • 31. GROUP BY The SQL GROUP BY is a clause that enables SQL aggregate functions for grouping of information. (ie.GROUP BY clause is used in collaboration with the SELECT statement to arrange identical data into groups.). This clause is used whenever aggregate functions by group are required. For example: 1. Display number of students in each class. SELECT count (*), class FROM student GROUP BY class; 2. Display sum of fees for each class. SELECT class, sum (fees) FROM student GROUP BY class;
  • 32. Having class: 'where' clause is used only to place condition on the selected columns, 'HAVING' clause is used to place condition on groups created by 'group by' clause, because here the 'WHERE' clause is not useable. Example: Display sum of fees which is more than 5000 for each class SELECT class, sum (fees) FROM student GROUP BY class HAVING sum (fees)>5000;
  • 33. DISTINCT : The DISTINCT keyword is used to remove duplicate values in a particular column. For example: Display classes in student table. SELECT class FROM student;
  • 34. UPDATE Command : This command is used to implement modification of the data values. Syntax: UPDATE <table name> SET <column name1>=new value, <column name>=new value etc [WHERE <condition>]; Example: 1. Increase fees value by 500. UPDATE student SET fees = fees + 500; 2. Select * from student ;
  • 35. Increase the fees value by 100 for adno 222. UPDATE student SET fees = fees+100 WHERE adno = 222;
  • 36. DELETE Command: This command is used to remove information from a particular row or rows. Note : This command will delete only row information but not the structure of the table. Syntax: DELETE FROM <table name> [WHERE <condition>]; For example: 1. Remove adno 444 information. DELETE FROM student WHERE adno = 444; 2. Select * from student;
  • 37. 2. Remove all records. DELETE FROM student; 3. Desc student; ALTER TABLE command This command is used to implement modification of the structure of the table. This is a DDL command. Using this command, we can add a new column, remove the existing column and modify data type of existing column. Syntax: ALTER TABLE <table name> [ADD/MODIFY/DROP] <column name>; For example: 1. Add one new column totalfees with number (10, 2). ALTER TABLE student ADD totalfees number(10,2); 2. Change totalfees datatype as number(12,2). ALTER TABLE student MODIFY totalfees number(12,2); 3. Remove totalfees column. ALTER TABLE student DROP totalfees;
  • 38. DROP Command: This command is used to remove the entire structure of the table and information. This is also from the DDL command. Syntax: DROP TABLE <table name>; For example: Remove the whole structure of student table. DROP TABLE student;
  • 39. Equi Join Equi Joins are used to give information in different tables. It is a special type of join in which we use only equality operator. For example SELECT * FROM product, customer WHERE product.product_no = customer. procuct_no; (or) SELECT * FROM product p, customer c WHERE p.product_no=c.procuct_no;
  • 42. NON EQUI JOIN The SQL NON EQUI JOIN uses comparison operator instead of the equal sign like >, <, >=, <= along with conditions. SYNTAX : SELECT * FROM table_name1, table_name2 WHERE table_name1.column [> | < |!= | >= | <= ] table_name2.column
  • 43. >Select * from product p, customer c where p.pdroduct_no != c.product_no; PRODUCT CUSTOMER