SlideShare a Scribd company logo
SQL
Create database <dbname>;
Create database learnersnote;
Opening the database
• Use <databasename>;
• Use learnernote;
CREATE TABLE table_name (
column1 datatype [(size)]
[constraint],
column2 datatype [(size)]
[constraint],
column3 datatype [(size)]
[constraint],
The CREATE TABLE statement is used to create a new table in a database.
MySQL Data Types
In MySQL there are three main data types:
string, numeric, and date and time.
Numeric Data Type
1. int – used for number without decimal.
2. Decimal(m,d) /Dec(m,d) – used for floating/real numbers. m denotes the total length of
number and d is number of decimal digits.
Date and Time Data Type
1. date – used to store date in YYYY-MM-DD format.
2. time – used to store time in HH:MM:SS format.
String Data Types
1. char(m) – used to store a fixed length string. m denotes max. number of characters.
2. varchar(m) – used to store a variable length string. m denotes max. no. of characters
Constraint
1.NOT NULL
2.UNIQUE
3.PRIMARY KEY
4.DEFAULT
5.CHECK
Structure of the table
Desc <tablename>;
INSERTING DATA INTO TABLE –
The rows are added to relations(table) using INSERT
command of SQL.
Syntax of INSERT is :
INSERT INTO <tablename>
VALUES ( <value1> , <value2> , …..) ;
INSERT INTO <tablename>(<column1><column2>,
….)
VALUES ( <value1> , <value2> , …..) ;
ALTER TABLE - ADD Column
To add a column in a table, use the following
syntax:
ALTER TABLE table_name
ADD column_name datatype;
ALTER TABLE - DROP COLUMN
To delete a column in a table, use the following
syntax (notice that some database systems
don't allow deleting a column):
ALTER TABLE table_name
DROP column_name;
ALTER TABLE - MODIFY DATATYPE
To change the data type of a column in a
table, use the following syntax:
ALTER TABLE table_name
MODIFY column_name datatype;
ALTER TABLE table_name
CHANGE COLUMN oldcolumn_name newcolumn_name
datatype;
ALTER TABLE - RENAME COLUMN
DROP TABLE:
DROP TABLE command allows to remove a table
from database. Once the DROP command is
issued, the table will no longer be available in the
database.
Syntax: DROP TABLE < table name>;
Select Command:
It helps to display the records as per our
requirement.
SELECT column1,column2….
FROM table_name ;
Different forms of select command:
1. Select * from employee;
It displays all rows and columns from the table.
For displaying particular rows.
Syntax: SELECT *
FROM <tablename>
WHERE <cond>;
Eg. Select *
from employee
where gender=’M’;
ELIMINATING REDUNDANT DATA
The distinct keyword is used to eliminate duplicate
records from the table.
Eg.
Select distinct (gender)
from employee;
USING COLUMN ALIASES
The columns that we select in a query can be given a different
name, i.e.column alias name for output purpose.
Syntax:
SELECT <columnname>AS column alias ,<columnname>AS column alias
FROM<tablename>;
Eg.select ecode as “EMP_Code”
from employee;
CONDITION BASED ON A RANGE
The BETWEEN operator defines a range of values that the
column values must fall into make the
condition true. The range include both lower value and
upper value.
e.g.To display ECODE,ENAME and GRADE of those
employees whose salary is between 40000 and
50000,command is:
SELECT ECODE , ENAME ,GRADE FROM EMPLOYEE
WHERE GROSS BETWEEN 40000 AND 50000;
CONDITION BASED ON A LIST
The in operator is used to display records based on a list of
values.
Eg. To display details of employees who have scored A,B and C
grades.
Select * from employee where grade in(‘A’,’B’,’C’);
Note: For displaying records that do not match in the list, we
have to use not in operator.
CONDITION BASED ON PATTERN MATCHES
LIKE operator is used for pattern matching in SQL. Patterns are described
using two special
wildcard characters: % and _ (underscore)
1. percent(%)– The % character matches any substring.
2. underscore(_)– The _ character matches any single character.
e.g.To display names of employee whose name starts with R in
EMPLOYEE table, the command is:
select ename from employee where ename like “R%”;
e.g. To display details of employee whose second character in name is:
select * from employee where ename like ‘_e%’;
SEARCHING FOR NULL
The NULL value in a column can be searched for in a table
using IS NULL in the WHERE clause.
E.g. to list employee details whose salary contain NULL, we
use the command:
Select * from employee where gross is null;
Note: For listing employees who earn salary, then it is:
Select * from employee where gross is not null;
Relational Operators
• To compare two values, a relational operator is used. The result of
the comparison is true or false.
Relational Operators recognized by SQL: =, >, <, <=, >=, <> (not
equal or !=)
Eg. Select * from employee where ecode <> 1001;
Above query will not display those employee details whose ecode
column value is 1001.
Logical Operators- (OR, AND, NOT)
1) To list the employee details having grades E2 or E3.
Select ecode, ename, grade, gross from employee where
(grade=‘E2’ OR grade=‘E3’);
Select * from employee where department=‘it’ or sal>50;
Select * from employee where not department=‘it’;
2) To list all the employees’ details having grades as ‘E4’
but with salary < 9000.
Sorting Results- ORDER BY clause
Results of SQL query can be sorted in a specific order using ORDER BY clause.
The ORDER BY clause allows sorting of query results by one or more columns. The sorting can
be done either in ascending or descending order.
Eg. Select * from emp order by ename;
Above query arranges the records in alphabetical order of ename value. By default order by
clause arranges in ascending order.
TO DISPLAY RECORDS IN DESCENDING ORDER
❖ Select * from employee order by ename desc;
Above query gives output in descending order of ename.
❖ Select * from employee ORDER BY grade DESC, ename ASC;
Above query displays records first in the descending order of grade and within the same
grade,
employees are displayed in the ascending order of Ename.
SQL AGGREGATE FUNCTIONS:
All the aggregate functions ignore null values except count(*).
Avg – to compute average value
Min – to find minimum value
Max – to find maximum value
Sum – to find total value
Count – to count non-null values in a column
Count( *) – to count total number of rows in a table including null values.
Examples:
Select avg(gross) from employee;
Select min(gross) from employee where deptno= 10;
Select count(*) from emp where gross> 10000;
Select count (DISTINCT gender) from employee;
DELETE FROM table_name WHERE condition;
Ad

More Related Content

Similar to SQL.pptx SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTER SCIENCE (083) Time allowed: 3 Hours Maximum Marks: 70 (20)

SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
Abdelhay Shafi
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
Abdelhay Shafi
 
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12  CBSESQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12  CBSE
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
sdnsdf
 
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12  CBSESQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12  CBSE
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
sdnsdf
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
Balqees Al.Mubarak
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
Balqees Al.Mubarak
 
SQL Data Manipulation language and DQL commands
SQL Data Manipulation language and DQL commandsSQL Data Manipulation language and DQL commands
SQL Data Manipulation language and DQL commands
sonali sonavane
 
SQL Data Manipulation language and DQL commands
SQL Data Manipulation language and DQL commandsSQL Data Manipulation language and DQL commands
SQL Data Manipulation language and DQL commands
sonali sonavane
 
My SQL.pptx
My SQL.pptxMy SQL.pptx
My SQL.pptx
KieveBarreto1
 
My SQL.pptx
My SQL.pptxMy SQL.pptx
My SQL.pptx
KieveBarreto1
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
Dev Chauhan
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
Dev Chauhan
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
ssuser0562f1
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
ssuser0562f1
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
ssuser0562f1
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
ssuser0562f1
 
MYSQL-database basic queries for good understanding
MYSQL-database basic queries for good understandingMYSQL-database basic queries for good understanding
MYSQL-database basic queries for good understanding
PriyadharshiniG41
 
MYSQL-database basic queries for good understanding
MYSQL-database basic queries for good understandingMYSQL-database basic queries for good understanding
MYSQL-database basic queries for good understanding
PriyadharshiniG41
 
Database Management System 1
Database Management System 1Database Management System 1
Database Management System 1
Swapnali Pawar
 
Database Management System 1
Database Management System 1Database Management System 1
Database Management System 1
Swapnali Pawar
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
Abdelhay Shafi
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
Abdelhay Shafi
 
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12  CBSESQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12  CBSE
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
sdnsdf
 
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12  CBSESQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12  CBSE
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
sdnsdf
 
SQL Data Manipulation language and DQL commands
SQL Data Manipulation language and DQL commandsSQL Data Manipulation language and DQL commands
SQL Data Manipulation language and DQL commands
sonali sonavane
 
SQL Data Manipulation language and DQL commands
SQL Data Manipulation language and DQL commandsSQL Data Manipulation language and DQL commands
SQL Data Manipulation language and DQL commands
sonali sonavane
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
Dev Chauhan
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
Dev Chauhan
 
MYSQL-database basic queries for good understanding
MYSQL-database basic queries for good understandingMYSQL-database basic queries for good understanding
MYSQL-database basic queries for good understanding
PriyadharshiniG41
 
MYSQL-database basic queries for good understanding
MYSQL-database basic queries for good understandingMYSQL-database basic queries for good understanding
MYSQL-database basic queries for good understanding
PriyadharshiniG41
 
Database Management System 1
Database Management System 1Database Management System 1
Database Management System 1
Swapnali Pawar
 
Database Management System 1
Database Management System 1Database Management System 1
Database Management System 1
Swapnali Pawar
 

Recently uploaded (20)

2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
Cultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptxCultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptx
UmeshTimilsina1
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
Cultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptxCultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptx
UmeshTimilsina1
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
Ad

SQL.pptx SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTER SCIENCE (083) Time allowed: 3 Hours Maximum Marks: 70

  • 1. SQL
  • 2. Create database <dbname>; Create database learnersnote;
  • 3. Opening the database • Use <databasename>; • Use learnernote;
  • 4. CREATE TABLE table_name ( column1 datatype [(size)] [constraint], column2 datatype [(size)] [constraint], column3 datatype [(size)] [constraint], The CREATE TABLE statement is used to create a new table in a database.
  • 5. MySQL Data Types In MySQL there are three main data types: string, numeric, and date and time. Numeric Data Type 1. int – used for number without decimal. 2. Decimal(m,d) /Dec(m,d) – used for floating/real numbers. m denotes the total length of number and d is number of decimal digits. Date and Time Data Type 1. date – used to store date in YYYY-MM-DD format. 2. time – used to store time in HH:MM:SS format. String Data Types 1. char(m) – used to store a fixed length string. m denotes max. number of characters. 2. varchar(m) – used to store a variable length string. m denotes max. no. of characters
  • 7. Structure of the table Desc <tablename>;
  • 8. INSERTING DATA INTO TABLE – The rows are added to relations(table) using INSERT command of SQL. Syntax of INSERT is : INSERT INTO <tablename> VALUES ( <value1> , <value2> , …..) ; INSERT INTO <tablename>(<column1><column2>, ….) VALUES ( <value1> , <value2> , …..) ;
  • 9. ALTER TABLE - ADD Column To add a column in a table, use the following syntax: ALTER TABLE table_name ADD column_name datatype;
  • 10. ALTER TABLE - DROP COLUMN To delete a column in a table, use the following syntax (notice that some database systems don't allow deleting a column): ALTER TABLE table_name DROP column_name;
  • 11. ALTER TABLE - MODIFY DATATYPE To change the data type of a column in a table, use the following syntax: ALTER TABLE table_name MODIFY column_name datatype;
  • 12. ALTER TABLE table_name CHANGE COLUMN oldcolumn_name newcolumn_name datatype; ALTER TABLE - RENAME COLUMN
  • 13. DROP TABLE: DROP TABLE command allows to remove a table from database. Once the DROP command is issued, the table will no longer be available in the database. Syntax: DROP TABLE < table name>;
  • 14. Select Command: It helps to display the records as per our requirement. SELECT column1,column2…. FROM table_name ; Different forms of select command: 1. Select * from employee; It displays all rows and columns from the table.
  • 15. For displaying particular rows. Syntax: SELECT * FROM <tablename> WHERE <cond>; Eg. Select * from employee where gender=’M’;
  • 16. ELIMINATING REDUNDANT DATA The distinct keyword is used to eliminate duplicate records from the table. Eg. Select distinct (gender) from employee;
  • 17. USING COLUMN ALIASES The columns that we select in a query can be given a different name, i.e.column alias name for output purpose. Syntax: SELECT <columnname>AS column alias ,<columnname>AS column alias FROM<tablename>; Eg.select ecode as “EMP_Code” from employee;
  • 18. CONDITION BASED ON A RANGE The BETWEEN operator defines a range of values that the column values must fall into make the condition true. The range include both lower value and upper value. e.g.To display ECODE,ENAME and GRADE of those employees whose salary is between 40000 and 50000,command is: SELECT ECODE , ENAME ,GRADE FROM EMPLOYEE WHERE GROSS BETWEEN 40000 AND 50000;
  • 19. CONDITION BASED ON A LIST The in operator is used to display records based on a list of values. Eg. To display details of employees who have scored A,B and C grades. Select * from employee where grade in(‘A’,’B’,’C’); Note: For displaying records that do not match in the list, we have to use not in operator.
  • 20. CONDITION BASED ON PATTERN MATCHES LIKE operator is used for pattern matching in SQL. Patterns are described using two special wildcard characters: % and _ (underscore) 1. percent(%)– The % character matches any substring. 2. underscore(_)– The _ character matches any single character. e.g.To display names of employee whose name starts with R in EMPLOYEE table, the command is: select ename from employee where ename like “R%”; e.g. To display details of employee whose second character in name is: select * from employee where ename like ‘_e%’;
  • 21. SEARCHING FOR NULL The NULL value in a column can be searched for in a table using IS NULL in the WHERE clause. E.g. to list employee details whose salary contain NULL, we use the command: Select * from employee where gross is null; Note: For listing employees who earn salary, then it is: Select * from employee where gross is not null;
  • 22. Relational Operators • To compare two values, a relational operator is used. The result of the comparison is true or false. Relational Operators recognized by SQL: =, >, <, <=, >=, <> (not equal or !=) Eg. Select * from employee where ecode <> 1001; Above query will not display those employee details whose ecode column value is 1001.
  • 23. Logical Operators- (OR, AND, NOT) 1) To list the employee details having grades E2 or E3. Select ecode, ename, grade, gross from employee where (grade=‘E2’ OR grade=‘E3’); Select * from employee where department=‘it’ or sal>50; Select * from employee where not department=‘it’; 2) To list all the employees’ details having grades as ‘E4’ but with salary < 9000.
  • 24. Sorting Results- ORDER BY clause Results of SQL query can be sorted in a specific order using ORDER BY clause. The ORDER BY clause allows sorting of query results by one or more columns. The sorting can be done either in ascending or descending order. Eg. Select * from emp order by ename; Above query arranges the records in alphabetical order of ename value. By default order by clause arranges in ascending order. TO DISPLAY RECORDS IN DESCENDING ORDER ❖ Select * from employee order by ename desc; Above query gives output in descending order of ename. ❖ Select * from employee ORDER BY grade DESC, ename ASC; Above query displays records first in the descending order of grade and within the same grade, employees are displayed in the ascending order of Ename.
  • 25. SQL AGGREGATE FUNCTIONS: All the aggregate functions ignore null values except count(*). Avg – to compute average value Min – to find minimum value Max – to find maximum value Sum – to find total value Count – to count non-null values in a column Count( *) – to count total number of rows in a table including null values. Examples: Select avg(gross) from employee; Select min(gross) from employee where deptno= 10; Select count(*) from emp where gross> 10000; Select count (DISTINCT gender) from employee;
  • 26. DELETE FROM table_name WHERE condition;
  翻译: