Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

SQL QUERIES

1. Creating a Database :
Syntax :- Create Database <Database Name> ;
Example:- Create database Student ;
2. To see list of Databases :
Syntax :- Show Databases;
3. To Select or Open any Database to
work :
Syntax:- Use Databasename ;
Example:- Use Student;
4. Creating a Table :
Syntax:- Create Table <tablename> (column name
datatype(size),Column name datatype(size),.......);
Example:- Create Table Record(Roll_No int , Name
char(30) , Stream varchar(15) , Percentage decimal(5,3));

5. Displaying the Table Structure :


Syntax:- Describe <Table name>;

or
Desc <Table name>;

Example:- Desc Record;


6. Inserting Rows in a Table :
Syntax:- insert into <tablename> values(value1,
value2,.......);

Example:- insert into Record values(1,'Petterson',


'Biology', 76.58);

7.Inserting Rows in a selected column of


a Table:
Syntax:- insert into <tablename>(column name)
values(value1,value2,.......);
Example:- insert into Record (Roll_No , Name,
Percentage) values(2,'Johan', 88.79);

8. Selecting Records or Rows in a Table :


Syntax:- select * from <table name>;
[for all records and all columns ]

or
select column name(s) from <table name>;
[for selected columns ]

Example:- select * from Record;


9. To Drop Table :
Syntax:- drop table <table name>;
Example:- drop table Record;
10. To add a column :
Syntax:- alter table <tablename> add columnname
datatype;
Example:- alter table Record add Rank int;
11. To Drop column :
Syntax:- alter table <tablename> drop column
columnname ;

Example:- alter table Record drop column Rank ;


12.To modify column datatype :
Syntax:- alter table <tablename> modify
columnname datatype;

Example:- alter table Record modify Percentage


decimal(6,3);

13. To add primary key :


Syntax:- alter table <tablename> add primary key
(column name);

Example:- alter table Record add primary key


(Roll_No);
14.To remove primary key :
Syntax:- alter table <tablename> drop primary key ;

Example:- alter table Record drop primary key ;


15. To rename table :
Syntax:- rename <old table name> to <new table
name>;

Example:- rename Record to Data;


16. Where Clause:
Syntax:- select columnname (s) from tablename
where <condition>;

Example:- select * from Record where Percentage <


90 ;

16. To drop database :


Syntax:- drop database <database name>;
Example:- drop database Student;
17. Distinct Clause:
Syntax:- select distinct column_name (s) from
tablename;

Example:- select distinct Roll_No from Record;


18. Operators in SQL :
 Arithmetic Operators(+,-,*,/,%):-
Syntax:- select <expression1> <arithmetic operator >
<expression 2> from <table name>;

Example:-select Roll_No, Name , Percentage +0.45


from Record;

 Comparison Operators(=,>,<,>=,<=,
<> or !=,!<,!>):-
Syntax:- select <column name> from <tablename>
where <expression> <comparison Operator>
<expression>;

Example:-select Roll_No,Name,Stream from Record


where Percentage =< 80.00;
 Logical Operators (AND, OR, NOT) :
Syntax:- select <column name> from <table name>
where <expressions> <boolean or logical operator>
<expressions>;

Example:- a. select Roll_No, Name , Class from


Record where Class = 10 or Class = 11;

b. select Roll_No, Name , Class from Record where not


Class = 10 ;

 IN Operator:-
Syntax:- select <column name(s)> from <table name>
where column name IN (value1,value2 ,...);

Example:- select name fron emp where empno IN


(3,5,7);

 BETWEEN Operator :-
Syntax:- select <column name(s)> from <table name>
where <column name> BETWEEN value 1 AND value 2;
Example:- select name from emp where empno
beteen 18000 and 30000;

 LIKE Operator:-
Syntax:- select <column name> from <table name>
where <column name> LIKE "condition";

a. Search for Employee whose name's second


letter is 'a':-
select * from empno where name like"_a%";

b.Search for Employee whose name ends with


'r':-
select * from empno where name like"%r";

c.Search for Employee whose dob is in Feb :-


select * from empno where dob like"%-02-%;

19. IS NULL Clause :


Syntax:- select <column name> from <table name>
where <column name> is null;

Example:- select * from Teacher where T_salary is


null;

20. IS NOT NULL Clause :


Syntax:- select <column name> from <table name>
where <column name> is not null;

Example:- select * from Teacher where T_salary is


not null;

21. AGGREGATE FUNCTIONS :


 MAX( ) :-
Syntax:- select max(column name) from table name;

Example:- a.select max(salary) from emp;


b. select max (salary) from emp where dept='sales';

 MIN( ) :-
Syntax:- select min(column name) from table name;
Example:- a.select min (salary) from emp;
b. select min (salary) from emp where dept='IT';

 AVG( ) :-
Syntax:- select avg(column name) from table name;

Example:- a.select avg (salary) from emp;


b. select avg (salary) from emp where dept='sales';

 SUM( ):-
Syntax:- select sum (column name) from table name;

Example:- a.select sum (salary) from emp;


b. select sum (salary) from emp where dept='sales';

 COUNT( ):-
Syntax:- select count (column name) from table
name;

Example:- a.select count (salary) from emp;


b. select count (salary) from emp where dept='HR';

c. select count (distinct dept) from emp;

d. select count(*) from emp;

22. GROUP BY :
Syntax:- select column name, aggregate functions
(column name) from table name where

condition group by column name;

Example:- select customer , sum(orderprice) from


orders group by customer;

23. HAVING CLAUSE :-


Syntax:- select column name, aggregate functions
(column name) from table name where

condition group by column name having aggregate

functions (column name) <condition>;

Example:- a. select customer , sum(orderprice) from


orders group by customer having sum(order price)<
3000;

b. select customer , sum(orderprice) from orders where


customer = 'Hansen' or customer = 'John' group by
customer having sum(order price)>1500;

24. ORDER BY Clause :


Syntax:- select <column name(s)> from table name
order by <column name (s)> asc/desc;

NOTE- "By default output will be


ascending order (asc) and to see output
in descending order we use desc clause."
Example:- a. select * from emp order by name;
b. select * from emp order by salary desc;

c. select * from emp order by dept asc , salary desc;

25. Update Statement :


Syntax:- update <table name> set column1 = value1 ,
column 2 = value 2,......... where<condition>;
Example:- update persons set address= 'storgt', city =
'sandness' where lastname= 'Hansen' and firstname =
'Ola';

26. Delete Statement:(delete rows in a table)


Syntax:- delete from <tabe name> where
<condition>;

Example:- a. delete from students where last name =


'Petterson' and first name = 'Kari';

b. {To delete all rows in atable}:-

delete from <table name>;

27. JOIN :
 NATURAL JOIN :-

Syntax:- select * from <table1> natural join <table 2>;

Example:- select * from Foods natural join Company


where Foods.Company_Id = Company .Company_Id;
 EQUI-JOIN:
Syntax:- select <column name(s)> from <table 1,
table2> where table1.column1 = table2.column1;

Example:- select * from Person, Orders where


Persons.P_Id = Orders.P_Id;

 CROSS JOIN :

Syntax:- select * from table 1 , table 2;

Example:- select * from Garment , Fabric;

28. To Display all the tuple in a column


in capital letter :
Syntax:- select upper(column name) from table name;
Example:- select upper (item_name) from
stationary;

29.To Display first (1,2,3,....) character of


every tuple in a column :
Syntax:- select left (column name,<no. of
character>)as extractstring from <table name>;

Example:- select left (customer name,5) as


extractstring from customers;

You might also like