/*
OPERATORS - There are 5 types of operators in SQL
1) Arithmetic Operator ---> + , -, * , /(Questient) , %(remainder)
2) Logical Operator ---> AND , OR , NOT
3) Comparision Operator --> == , != , < , > , <= ,>=
4) Range Operator ---> Between , Not Between
5) String Operator ---> Like , Not Like
*/
/* Creating Student Database*/
CREATE Database Student_Database
USE Student_Database
/* Creating Student Table */
CREATE Table Student (
stno int,
stname varchar(15),
sub1 int,
sub2 int,
sub3 int
)
/* INSERT Records in the Table */
INSERT INTO Student Values (002,'Akshay',80,90,95)
/* ARITHMETIC OPERATORS */
-- WAQ to Display Student Details
SELECT * FROM Student
-- WAQ to Display student details along with total Marks
SELECT *, (sub1 + sub2 + sub3) as 'Total' from Student
-- WAQ to display details along with Total Marks and % of Marks
SELECT *, (sub1 + sub2 + sub3) as 'Total' , (sub1 + sub2 + sub3)/3 as 'Percentage'
from Student
/* LOGICAL OPERATORS */
CREATE Table Student_Details (
stno int,
stname varchar(15),
subject varchar(15),
Marks int
)
INSERT into Student_Details Values (1,'Akshay','Maths','80'),
(1,'Akshay','English','80'),
(2,'Avinash','Maths','90'),
(3,'Dhruv','English','60'),
(4,'Mohit','SQL','95'),
(5,'Rajat','English','65'),
(6,'Payal','Maths','70'),
(7,'Sujith','SQL','100')
SELECT * FROM Student_Details
-- WAQ to display student who have opted for SQL
SELECT * FROM Student_Details where subject = 'SQL'
-- WAQ to display student who have opted for SQL and Maths
SELECT * FROM Student_Details where subject = 'SQL' OR subject = 'Maths'
-- WAQ to display student whose name is Akshay and opted for Maths
SELECT * FROM Student_Details where stname = 'Akshay' AND subject = 'Maths'
-- WAQ to display students who do not have Maths
SELECT * FROM Student_Details where NOT subject = 'Maths'
-- WAQ to display students who do not have Maths and English
SELECT * FROM Student_Details where NOT subject in ('Maths','English')
/* COMPARISION OPERATORS */
-- WAQ to display Students whose Marks are greater than 90
SELECT * FROM Student_Details Where Marks > 90
-- WAQ to display Students whose Marks are greater than equal to 90
SELECT * FROM Student_Details Where Marks >= 90
-- WAQ to display Students whose Marks are less than equal to 70
SELECT * FROM Student_Details Where Marks <= 70
-- WAQ to display Students whose Marks are not equal to 90
SELECT * FROM Student_Details Where Marks != 90
/* RANGE OPERATORS */
-- WAQ to display students whose Marks are between 80 and 100
SELECT * FROM Student_Details where Marks between 80 and 100
-- WAQ to display students whose Marks are between 80 and 100 excluding 80 and 100
SELECT * FROM Student_Details where Marks >80 and Marks < 100
-- WAQ to display students whose Marks are Not between 80 and 100
SELECT * FROM Student_Details where Marks Not between 80 and 100
/* STRING OPERATORS */
-- WAQ to display students whose name starts with A
SELECT * FROM Student_Details where stname like 'A%'
-- WAQ to display students whose name starts with A and ends with Y
SELECT * FROM Student_Details where stname like 'A%Y'
-- WAQ to display students whose name contains Ji
SELECT * FROM Student_Details where stname like '%ji%'
-- WAQ to display students whose name has 5 Letters
SELECT * FROM Student_Details where stname like '_____'
-- WAQ to display students whose name has 5 Letters and ends with t
SELECT * FROM Student_Details where stname like '____t'
/* CLAUSES IN SQL - By using Clauses we can provide some Additional facilities to
the query.
Therev are 5 types of Clauses in SQL :-
1) Where
2) Group By
3) Having
4) Order By
5) Top N
*/
/* Where Clause */
-- WAQ to display students whose marrks are grater than 90
SELECT * FROM Student_Details where Marks > 90
/* GROUP BY CLAUSE -
1) It is used in collaboration with Select statement to arrange identical data into
groups
2) Group by returns one record of each group
3) It involves use of aggregate function such as count , sum , average , max
*/
-- WAQ to know the average marks scored in each subject
SELECT subject , AVG(Marks) as 'AVerage_Marks'
FROM Student_Details
GROUP BY subject
-- WAQ to know the count of each subject
SELECT subject , count(*) as 'Total_Count'
FROM Student_Details
GROUP BY subject
-- WAQ to display maximum marks obtained in each subject
SELECT subject , max(Marks) as 'Max_Marks'
FROM Student_Details
GROUP BY subject
/* HAVING CLAUSE -
1) Having is also used to filter Data like where Clause
2) BUT Having applied on the group by records
3) Having will work when group by clause is present
*/
-- WAQ to know the average marks scored in each subject and average marks should be
greater than equal to 80
SELECT subject , AVG(Marks) as 'AVerage_Marks'
FROM Student_Details
GROUP BY subject
SELECT subject , AVG(Marks) as 'AVerage_Marks'
FROM Student_Details
GROUP BY subject
HAVING AVG(Marks) >= 80
-- WAQ to know the count of each subject and count should be grrater than 2
SELECT subject , count(*) as 'Total_Count'
FROM Student_Details
GROUP BY subject
HAVING count(*) > 2
/*
----- DIFFERENCE BETWEEN WHERE AND HAVING -------------------
1) Where clause do not applies on aggregates whereas Having is applied on
Aggregates
2) Where comes before group by , however having comes after group by
3) We can use where clause without group by whereas we cannot use having without
group by
*/
/* FLOW OF WHERE --> GROUPBY ---> HAVING */
SELECT * FROM Student_Details
SELECT * FROM Student_Details where subject in ('Maths','English')
SELECT subject, Marks FROM Student_Details where subject in ('Maths','English')
SELECT subject, Avg(Marks)
FROM Student_Details
where subject in ('Maths','English')
group by subject
SELECT subject, Avg(Marks) as 'Average Marks'
FROM Student_Details
where subject in ('Maths','English')
group by subject
having Avg(Marks) >= 80
/* Order BY Clause
1) It will arrange the data either in ascending or descending order
*/
-- WAQ to display students in order of their Marks
SELECT * FROM Student_Details ORDER BY Marks
-- WAQ to display students in order of their Marks in descending order
SELECT * FROM Student_Details ORDER BY Marks desc
-- WAQ to display students in order of their Marks obtained in descending order
only for Maths and English
SELECT * FROM Student_Details where subject in ('Maths','English') ORDER BY Marks
desc
/* TOPN Clause
1) It will select top N records from the table
*/
SELECT Top 5* from Student_Details
SELECT Top 2* from Student_Details
SELECT top 2* FROM Student_Details where subject in ('Maths','English') ORDER BY
Marks desc