0% found this document useful (0 votes)
20 views32 pages

SQL Basics: Constraints in SQL: 1) Not Null

The document provides an overview of SQL basics, including constraints, SELECT queries, aggregate functions, and JOIN operations. It explains how to create tables, apply conditions, and perform various types of joins such as INNER, LEFT, RIGHT, and SELF JOIN. Additionally, it covers the use of GROUP BY, HAVING clauses, and ranking functions like ROW_NUMBER, DENSE_RANK, and RANK.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views32 pages

SQL Basics: Constraints in SQL: 1) Not Null

The document provides an overview of SQL basics, including constraints, SELECT queries, aggregate functions, and JOIN operations. It explains how to create tables, apply conditions, and perform various types of joins such as INNER, LEFT, RIGHT, and SELF JOIN. Additionally, it covers the use of GROUP BY, HAVING clauses, and ranking functions like ROW_NUMBER, DENSE_RANK, and RANK.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

SQL BASICS

Constraints in SQL:
1) not null;
does not accept null values, must put any value , but not null.
2) unique;
no duplicates allow, all values in the column must be different
3) check;
we give condition, if the input value satisfies the condition, then it can be store
in data base, other wise it doesn’t store.
Example: lets try to create a table with the above constraints.

Now try to insert valid values


Now try to insert invalid values

here we tried to insert duplicate gmail and age lessthan 12, so it is throwing
error .

SELECT QUERY AND WHERE CLAUSE.


Table:

1) equality (=)
select * from student where id < 15;

2) less than or greater than (< , >)


select * from student where id < 15;

3) less than or equal to (<=)


select * from student where age >= 15;

4) between
Select * from student where between 15 and 18;

5) IN
Select * from student where age in (10,17,18);

6) LIKE (%s) /* prints the data which has “s” as the last character;
LIKE (s%) /* prints the data which has “s” as the first character;
LIKE (%s%) /* prints the data where “s” occurs in the data;

7) <> (greater than and less than)


Select * from student where age <> 18;
(it prints the row where age is less than 18 and greater than 18 )

AGGREGATE FUNCTIONS:
COUNT (number of entities)
Select count(*) as no_of _students from student;

It means there are 17 rows or records in student table.


we can apply conditions also.
Select count(*) as no_students_age_is_18 from student where age =
18;

It means there are 11 number of students who’s age is 18.


AVERAGE(avg(marks))
Select avg(marks) as marks_avg from students;

It prints the average of marks column.

We can apply condition like we can print the average of marks of


students who’s age is 18.
Select avg(marks) from students where age = 18;
ADDITION(sum(marks))
Select sum(marks) from student where age = 18;

It sums up the marks of students who’s age is 18.


MAXIMUM(max(marks))
Select max(marks) from student where age =18;

it prints the marks of student who got highest and who’s having age
18.

MINIMUM(min(marks))
it prints the marks of student who got least and who’s having age 18.
DISTINCT
Select distinct age from student;

these are the total


students and their ages.

But we want just what aged students are there.

Select distinct age from student;


it means in entire table
there are students who are
aged , 18,19,17. Except this
ages, no other aged people
are not there .

We can apply aggregate functions also like this.


Select avg(distinct(age)) from student; 18 (average of 18,19,17)
Select min(distinct(age)) from student;17 (the least age of all ages)
Select max(distinct(age)) from student;19 (the max age of all ages)

JOINS
LETS CREATE TABLES LIKE FOLLOWING ONE:

DESCRIPTION OF TABLES:
Primary key and int
varchar(30)

int

Int

Int
Int and Foreign key references student_table(id)

varchar

Int and Foreign key references student_table(id)

varchar

Int and Foreign key references student_table(id)

Creation of table:
We can rename a table name :
Rename table marks to marks_table;

Creation of sports_table
Creation of ncc_nss_table.
INNER JOIN (INTERSECTION OF 2 TABLES)

Lets try to get a new table in which the details of only students who
play sports.
we have to get the common students in student_table and students
in sports_table… but in sports_table we have only student_id in
sports_table. So, we have to take common id in student_table and id in
sports table.
Now we have to print only the id, student name, age of student_table
and sports name in sports_table.

LEFT JOIN

it prints the intersection


part between table 1 and
table 2 along with all values
in table 1. If the data in
table 1 is not there in table
2 , then it filled with null.
Example : retrieve the all students details who are not in ncc_nss club.
first left join the table 1 and table 2, so, that who are not in ncc_nss
are filled with null

now select only the student who are null in ncc_nss :


RIGHT JOIN

it prints the


intersection part
between table 1 and
table 2 along with all
values in table 2. If the
data in table 2 is not
there in table 1 , then it
filled with null.

Example : retrieve the student_id’s who are in ncc but not in sports.
lets do right join on sports table and ncc_nss_table.
This table means that student who has id as 1 is in ncc and cricket
But student who has id as 2 is in ncc but not in any other sport, so the
sports column filled with null.

This table means that student who has id as 2 and 8 are in nss
but not in any other sport, so the sports column filled with null.
RIGHT JOIN
(There is no direct query for full outer join in MySQL, but we can do
UNION on right join and left join)

it prints the intersection part between table 1 and table 2 along with
all values in table 1 and table 2. If the data in table 1 is not there in
table 2 , then it will be filled with null AND If the data in table 2 is not
there in table 1 then it will be filled with null

SELECT *
FROM table1
LEFT JOIN table2 ON table1.id = table2.id

UNION

SELECT *
FROM table1
RIGHT JOIN table2 ON table1.id = table2.id
WHERE table1.id IS NULL;
Explanation:
Student who has id as 5 & 7 are in basketball and volleyball respectively
but not in ncc_nss club.
Student who has id as 2 & 8 are in nss but not in any other sport.
SELF JOIN
(here we join the table with itself with inner, left, right according to
our necessary)

I have 2 tables (student_tables, marks_tables)

Now I want to get the data of id, name, marks along with this condition
(I want to know the how many students or marks are there which are
less than each marks) for example in a table if I have these marks
(23,25,26,24,29,39) I want to get the result that row of 23 marks will
join with the all rows in which the marks is less than 23. In the same
way the row of marks 25 will join with the rows of all marks which are
less than 25.. in the same way all marks are get joined with the rows of
marks which are less than its marks.
Ex: (row of 23 marks does not get joined with any other row because
there is no mark les than 23. ) , (row of 25 mark will get joined with row
of marks 23 and 24, so two rows are created, one row is 25 join with
23 and other row is 25 join with 24)
Approach
(student_table as t1 inner join marks_table as t2 on t1.id = t2.id ) as t1
Inner join (student_table as t1 inner join marks_table as t2 on t1.id =
t2.id) as t2 on t1.marks < t2.marks.
Visualization:

INNER JOIN
QUERY:

Still result is
there but no space to paste.
ORDER BY (ASCENDING ORDER (asc) , DESCENDING ORDER (desc))

 STUDENT_TABLE

 MARKS_TABLE
ORDER BY MARKS IN DESCENDING ORDER

Table is ordered by marks in


descending order But here student 2, student 3, student 4, student 5
has same marks, so now we have to order those students according to
their age.
Here the student 2,3,4,5
who got same marks 95 , are
ordered by age in descending
order.
But even if ages are same ,
we can orer it b names in
alphabetical ascending order
or descending order.

But even if ages are


same , we can orer it
b names in
alphabetical
ascending order or
descending order.
GROUP BY
It is used to make elements to get grouped.
Student_marks (table)

Now find how many students are in cricket group, football group,
basketball group.
Syntax:
Select sports, count(name) as count_of_students from student_marks
Group by sports;
Now find what age groups are in different sports group, and how
many?
Select sports,age, count(name) from student_sports group by
sports,age;

 There are 2 students who


are 18 and in cricket.

 There are 3 students who


are 17 and in cricket.

HAVING CLAUSE
(after applying the (group by ), we apply having condition on group.)
Ex: I want know how many students are in different sports group and
with their average marks (ex: no_of_students in cricket group and their
average marks, no_of_students in basketball, and their average marks),
Print only if the no.of students in
certain sport group have more than 2
memebers. (ex: if cricket group has
only 2 or 1 member do not select it.)
This is actual result group by sports with count(name) before
applying having condition.

Select sports, count(name) as no_students , avg(marks) as avg_marks


from student_sports group by sports having no_students > 2;

 This is result group


by sports with
count(name) after
applying having
condition
(no_of_students > 2)

 This is result group


by sports with
count(name) after
applying having
condition
(no_of_students > 2)
And (avg_marks > 80)
It means the students in cricket group are more than 2 members and
has their avg_mark is greater than 80.

RANK [row_number(), rank(), dense_rank]


Row_number(): if the scores are same , generally the same scores are to be
assigned with same rank, but row_number() gives high rank for the score which
is recorded first in original table.
(Rahul 80 marks, joshi 80 marks, here both of then got same marks but
rank 1 goes to the Rahul as he is in the first row in table and rank2 goes to the
joshi as he is in the second row in table. )

This is actual table.

Applying row_number():
Select id,name,score, row_number() over(order by score desc) from
stu_rank;
DENSE_RANK():
It gives rank generally. For same marks same rank.
Select id,name,score, dense_rank() over(order by score desc) from
stu_rank;

RANK():
It gives same rank to the same marks but next rank number depends
on the number of same rank given to its just high score.(89,89,89,50:
rank 1 will given to the 89, then for 50 rank 4 will be given as the rank 1
given to all three 89 marks.)
Select id,name,score, rank() over(order by score desc) from stu_rank;

You might also like