SQL Revision

You might also like

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

SQL Revision

Q) To display the details of all the passengers.


Ans:
Select *
From Passenger ;

Select * From Passenger ;

Q) To display Passenger no and name of all the


passengers.
Ans:
Select PNO, Name
From Passenger ;

Select PNO And Name


From Passenger ;

Select PNO, Name From Passenger ;


Where : To extract specific rows.

Q) To display those flight details starting with „Delhi‟.


Ans:
Select *
From Flight
Where Start = “delhi” ;

Relational operators
>, <, >=, <=, =, !=

Logical Operators
And, Or, Not

Q) To display the details of those flight starting with


Delhi and whose destination is „Mumbai‟.
Ans:
Select *
From Flight
Where Start = “Delhi” And End = “Mumbai” ;

Date : After >, Before <

Q) To display the details of those flight whose flight


date is after „2021-05-20‟.
Ans:
Select *
From Flight
Where F_Date > “2021-05-20” ;

Distinct : To display the different values of a column.


It is used to eliminate the redundancy from the
result of select command

Q)To display all the different endings from flight table.


Ans:
Select Distinct End
From Flight
Chennai
Bengaluru
Mumbai

NULL : Empty
With NULL IS NULL/ IS NOT NULL operator is used.
= operator is not used.

Q) To display the details of those flights whose fare is


not entered/known.
Ans:
Select *
From Flight
Where Fare IS NULL ;

Q) To display the details of those flights whose fare is


entered/known.
Ans:
Select *
From Flight
Where Fare IS NOT NULL ;

Order By
Q) To display the details of all the flights in ascending
order of fare.
Ans:
Select *
From Flight
Order By Fare Asc;

Q) To display the details of all the flights in


descending order of fare.
Ans:
Select *
From Flight
Order By Fare Desc;

Q) To display the details of all the flights whose origin


is Delhi in descending order of fare.
Ans:
Select *
From Flight
Where Start = “Delhi”
Order By Fare Desc;

Q) To display the details of all the flights whose origin


is Delhi and fare is more than 10000 in descending order
of fare.
Ans:
Select *
From Flight
Where Start = “Delhi” And Fare > 10000
Order By Fare Desc;

Like :
% : It is used to replace none, one or more characters.
_ : It is used to replace only single character.

“M%” : Starting with M


“%M” : Ending with M
“%M%” : Name contain M ( M at any place)
“_M%” : M at 2nd place.
“__M%” : M at 3rd place.
“______” : name contain only six characters.
“%Kumar” : Ending with Kumar

Q) To display the details of those passengers whose name


start „S‟.
Ans:
Select *
From Passenger
Where name Like “S%” ;

Q) To display the name of those female passengers whose


name ends with „verma‟.
Ans:
Select Name
From Passenger
Where Gender = “Female” And Name Like “%Verma” ;

Q) To display the name of those female passengers whose


name does not ends with „verma‟.
Ans:
Select Name
From Passenger
Where Gender = “Female” And Name Not Like “%Verma”;

Between : It is used to specify range.

Q) To display the details of those flights whose fare is


in the range of 5000 to 10000.
Ans:
Select *
From Flight
Where fare Between 5000 And 10000 ;

Q) To display the details of those flights that are


ending at “Mumbai” and fare is in the range of 5000 to
10000.
Ans:
Select *
From Flight
Where End = “Mumbai” And fare Between 5000 And 10000 ;

Q) To display the details of those flights that are


ending at “Mumbai” and fare is not in the range of 5000
to 10000.
Ans:
Select *
From Flight
Where End = “Mumbai” And fare Not Between 5000 And 10000;
Q) To display the details of those flights where year of
flight date 2015.
Ans
Select *
From flight
Where F_Date Between “2015-01-01” And “2015-12-31” ;

IN : it is used to specify list of values.

Q) To display the flight no and fare of those flights


whose starting is from delhi, mumbai or Chennai.
Ans:
Select FNO, Fare
From Flight
Where Start IN(“Delhi”, “Mumabi”, “Chennai” );
OR

Ans:
Select FNO, Fare
From Flight
Where Start = “Delhi” OR Start = “Mumabi” OR Start =
“Chennai” ;

Q) To display the flight no and fare of those flights


whose starting is from delhi, mumbai or Chennai and
ending at “bengaluru”.
Ans:
Select FNO, Fare
From Flight
Where Start IN(“Delhi”, “Mumabi”, “Chennai” ) And End
= “Bengaluru” ;

Q) To display the flight no and fare of those flights


whose starting is other than delhi, mumbai or Chennai.
Ans:
Select FNO, Fare
From Flight
Where Start NOT IN(“Delhi”, “Mumabi”, “Chennai” );
Update : It is used to modify the data of a table.

Q) To change the fare to 6500 of flight no „F103‟.


Ans:
Update Flight
Set Fare = 6500
Where FNO = “F103” ;

Q)To increase the fare of all the flights by 5%.


Ans:
Update Flight
Set Fare = fare + Fare*0.05 ;

Q) To display the flight no and increased fare by 500 of


those flights that are starting from delhi.
Ans:
Select FNO, Fare+500
From Flight
Where Start= “delhi “ ;

Delete : it is used to delete the data/row from the


table.
Delete from Flight ;

Q) To delete the details of those flight that are ending


at „mumbai‟.
Ans:
Delete from flight
Where End = “Mumbai” ;
Drop Table : To delete table structure
Drop Table Flight ;

Describe/Desc : it is used to display the table


structure.

Desc Flight;

Multi Row Functions/Group Functions/Aggregate Functions


Max(), Min(), Count(), Avg(), Sum()

Q) To display the largest amount of fare.


Ans:
Select max(Fare)
From Flight ;

5500

Q) To display the maximum and minimum fare amount of


flights ending at Chennai.
Ans:
Select Max(Fare), Min(Fare)
From Flight
Where End = “Chennai” ;

Max(Fare) Min(Fare)
5500 4500
Count() : To count/display number of something

Count(*) : it counts number of rows


Count(col_name) : It counts not null values of the
column.

Select Count(*) from flight ;


5

Select Count(Fare) from flight;


4

Select Count(F_Date) from flight;


5

Q) Select Count(*) from Flight;


Select Count(Fare) from flight;

Above two statements are giving different outputs. What


may be the possible reason.

Ans:
If Above two statements are giving different outputs,
it means there are some null values in Fare column.

Group By : it is used to divide the rows of a table into


different groups.
Q) To display the maximum and minimum fare start wise.
Q) To display the maximum and minimum fare of each start.
Ans:
Select Start, Max(Fare), Min(Fare)
From Flight
Group By Start ;

Start Max(Fare) Min(Fare)


Mumbai 4500 4000
Delhi 5500 5000
Kolkata 4500 4500

Q) To display average fare and number of flights


Destination wise.
Ans:
Select End, Avg(fare), Count(*)
From Flight
Group By End;

End Avg(Fare) Count(*)


Chennai 5000 2
Bengaluru 2
Mumbai 1

Q) To display average fare and number of flights whose


destination is Chennai and Bengaluru.
Ans:
Select End, Avg(fare), Count(*)
From Flight
Group By End
Having End IN(“Chennai”, “Bengaluru”);

You might also like