Sql-Most Important Concepts Placement Preparation: (Save and Share)
Sql-Most Important Concepts Placement Preparation: (Save and Share)
PLACEMENT PREPARATION
[EXCLUSIVE NOTES]
TOPICS COVERED-
PART-1 :-
➢ SELECT Query In SQL
➢ Distinct Clause In SQL
➢ INSERT INTO Query In SQL
➢ INSERT INTO Statement In SQL
➢ DELETE Statement In SQL
➢ UPDATE Statement In SQL
➢ SELECT TOP Clause In SQL
➢ ORDER BY In SQL
➢ Aliases In SQL
➢ Wildcard operators In SQL
➢ Join (Inner, Left, Right and Full Joins)
➢ CREATE In SQL
HIMANSHU KUMAR(LINKEDIN)
https://www.linkedin.com/in/himanshukumarmahuri
SELECT Query :-
Select is the most commonly used statement in SQL. The SELECT
Statement in SQL is used to retrieve or fetch data from a database. We
can fetch either the entire table or according to some specified rules.
The data returned is stored in a result table. This result table is also
called result-set.
The select clause is the first clause and is one of the last clauses of the
select statement that the database server evaluates. The reason for this
is that before we can determine what to include in the final result set,
we need to know all of the possible columns that could be included in
the final result set.
Sample Table:
Basic Syntax:
SELECT column1,column2 FROM table_name
column1 , column2: names of the fields of the table
This query will return all the rows in the table with fields column1 ,
column2.
• Query to fetch the fields ROLL_NO, NAME, AGE from the table
Student:
Output:
1 Ram 18
2 RAMESH 18
3 SUJIT 20
4 SURESH 18
Output:
Distinct Clause :-
Syntax :
SELECT DISTINCT column1, column2
FROM table_name
Queries
FROM Student;
Output :
NAME
Ram
RAMESH
SUJIT
SURESH
SELECT DISTINCT *
FROM Student;
Output :
Queries:
Method 1 example:
INSERT INTO Student VALUES ('5','HARSH','WEST BENGAL','875
9770477','19');
Output: The table Student will now look like:
Queries:
Method 1 (Inserting only values) :
INSERT INTO Student VALUES ('5','HARSH','WEST
BENGAL','XXXXXXXXXX','19');
Output: The table Student will now look like:
ROLL_NO NAME ADDRESS PHONE Age
We have used the SELECT statement to copy the data from one
table and INSERT INTO statement to insert in a different table.
Table2: LateralStudent
Output: This query will insert all the data of the table LateralStudent in
the table Student. The table Student will now look like,
ROLL_N Ag
NAME ADDRESS PHONE
O e
XXXXXXXXX
1 Ram Delhi 18
X
XXXXXXXXX
3 SUJIT ROHTAK 20
X
XXXXXXXXX
4 SURESH Delhi 18
X
XXXXXXXXX
3 SUJIT ROHTAK 20
X
ROLL_N Ag
NAME ADDRESS PHONE
O e
XXXXXXXXX
7 SOUVIK DUMDUM 18
X
XXXXXXXXX
8 NIRAJ NOIDA 19
X
SOMES XXXXXXXXX
9 ROHTAK 20
H X
Output: This query will insert the data in the columns ROLL_NO,
NAME and Age of the table LateralStudent in the table Student
and the remaining columns in the Student table will be filled
by null which is the default value of the remaining columns. The
table Student will now look like,
Output: This query will select only the first row from table
LateralStudent to insert into the table Student. The table Student
will now look like,
(Value1, Value2,Value3,.....),
(Value1, Value2,Value3,.....),
............................. ;
table_name: name of the table
Example:
The following SQL statement insert multiple rows in Student Table.
Input :
INSERT INTO STUDENT(ID, NAME,AGE,GRADE,CITY) VALUES(1,"AMI
T KUMAR",15,10,"DELHI"),
(2,"GAU
RI RAO",18,12,"BANGALORE"),
(3,"MAN
AV BHATT",17,11,"NEW DELHI"),
(4,"RI
YA KAPOOR",10,5,"UDAIPUR");
Output : STUDENT TABLE This query will insert all values in each
successive row in the STUDENT TABLE . Thus STUDENT Table will look
like this:
ID NAME AGE GRADE CITY
DELETE Statement –
The DELETE Statement in SQL is used to delete existing records from a
table. We can delete a single record or multiple records depending on
the condition we specify in the WHERE clause.
Basic Syntax:
DELETE FROM table_name WHERE some_condition;
Sample Table:
Example Queries:
Output: The above query will delete only the first row and the
table Student will now look like,
fifth row).
Output: The above query will delete two rows(third row and fifth
row) and the table Student will now look like,
Output: All of the records in the table will be deleted, there are
no records left to display. The table Student will become empty!
UPDATE Statement –
The UPDATE statement in SQL is used to update the data of an existing
table in database. We can update single columns as well as multiple
columns using UPDATE statement as per our requirement.
Basic Syntax-
UPDATE table_name SET column1 = value1, column2 = value2,.
..
WHERE condition;
table_name: name of the table
column1: name of first , second, third column....
value1: new value for first, second, third column....
condition: condition to select the rows for which the
values of columns needs to be updated.
NOTE: In the above query the SET statement is used to set new values
to the particular column and the WHERE clause is used to select the
rows for which the columns are needed to be updated. If we have not
used the WHERE clause then the columns in all the rows will be
updated. So the WHERE clause is used to choose the particular rows.
Example Queries
Output: This query will update two rows(third row and fifth row) and
the table Student will now look like,
ROLL_N Ag
NAME ADDRESS PHONE
O e
XXXXXXXXX
1 Ram Delhi 18
X
XXXXXXXXX
3 PRATIK ROHTAK 20
X
XXXXXXXXX
4 SURESH Delhi 18
X
XXXXXXXXX
3 PRATIK ROHTAK 20
X
Output
The above query will update two columns in the first row
and the table Student will now look like,
ROLL_N Ag
NAME ADDRESS PHONE
O e
XXXXXXXXX
1 PRATIK SIKKIM 18
X
XXXXXXXXX
3 PRATIK ROHTAK 20
X
XXXXXXXXX
4 SURESH Delhi 18
X
XXXXXXXXX
3 PRATIK ROHTAK 20
X
Basic Syntax:
Queries
Output:
XXXXXXX
1 Ram Delhi 18
XXX
Output:
ROLL_ ADDRES Ag
NAME PHONE
NO S e
XXXXXXXX
1 Ram Delhi 18
XX
XXXXXXXX
3 SUJIT ROHTAK 20
XX
NOTE: To get the same functionality on MySQL and Oracle databases
there is a bit of difference in the basic syntax;
ORDER BY –
The ORDER BY statement in SQL is used to sort the fetched data in either
ascending or descending according to one or more columns.
Syntax:
SELECT * FROM table_name ORDER BY column1 ASC|DESC , colum
n2 ASC|DESC
Now consider the above database table and find the results of different
queries.
Sort according to a single column:
In this example, we will fetch all data from the table Student and sort the
result in descending order according to the column ROLL_NO.
Query:
SELECT * FROM Student ORDER BY ROLL_NO DESC;
Output:
In the above output, we can see that first the result is sorted in ascending
order according to Age. There are multiple rows of having the same Age.
Now, sorting further this result-set according to ROLL_NO will sort the
rows with the same Age according to ROLL_NO in descending order.
Note:
ASC is the default value for the ORDER BY clause. So, if we don't specify
anything after the column name in the ORDER BY clause, the output will
be sorted in ascending order by default.
Take another example of the following query will give similar output as
the above:
Query:
SELECT * FROM Student ORDER BY Age , ROLL_NO DESC;
Output:
(5,'SAPTARHI','KOLKATA',9193789625,19),
(2,'PRATIK','BIHAR',9193457825,19),
(6,'DHANRAJ','BARABAJAR',9193358625,20),
(3,'RIYANKA','SILIGURI',9193218625,20);
SELECT Name, Address
FROM studentinfo
ORDER BY 1
Output:
Aliases –
Aliases are the temporary names given to table or column for the
purpose of a particular SQL query. It is used when name of column or
table is used other than their original names, but the modified name is
only temporary.
The renaming is just a temporary change and table name does not
change in the original database.
Aliases are useful when table or column names are big or not very
readable.
These are preferred when there are more than one table involved in a
query.
Basic Syntax:
Output:
CODE
To fetch Branch using Stream as alias name and Grade as CGPA from
table Student_Details.
Output:
Stream CGPA
Information Technology O
Computer Science E
Computer Science O
Mechanical Engineering A
Generally table aliases are used to fetch the data from more than just
single table and connect them through the field relations.
Output:
NAME Grade
SUJIT O
Please write comments if you find anything incorrect, or you want to share more
information about the topic discussed above.
Wildcard operators –
Prerequisite: SQL | WHERE Clause In the above mentioned article
WHERE Clause is discussed in which LIKE operator is also explained,
where you must have encountered the word wildcards now lets get
deeper into Wildcards.
Wildcard operators are used with LIKE operator, there are four basic
operators:
Operator Description
Basic syntax:
SELECT column1,column2 FROM table_name WHERE column LIKE w
ildcard_operator;
column1 , column2: fields in the table
table_name: name of table
column: name of field used for filtering data
Queries
To fetch records from Student table with NAME ending with letter 'T'.
Output:
To fetch records from Student table with NAME ending any letter but
starting from 'RAMES'.
Output:
2RAMESHGURGAONXXXXXXXXXX18
Output:
2RAMESHGURGAONXXXXXXXXXX18
Output:
• To fetch records from Student table with PHONE field having a '9'
in 1st position and a '5' in 4th position.
Output:
Output:
Output:
• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• FULL JOIN
A. INNER JOIN
The INNER JOIN keyword selects all rows from both the tables as long
as the condition is satisfied. This keyword will create the result-set by
combining all rows from both the tables where the condition satisfies
i.e value of the common field will be the same.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.
table2: Second table
matching_column: Column common to both the tables.
Note: We can also write JOIN instead of INNER JOIN. JOIN is same as
INNER JOIN.
This query will show the names and age of students enrolled in
different courses.
ON Student.ROLL_NO = StudentCourse.ROLL_NO;
Output:
B. LEFT JOIN
This join returns all the rows of the table on the left side of the join and
matches rows for the table on the right side of the join. For the rows for
which there is no matching row on the right side, the result-set will
contain null. LEFT JOIN is also known as LEFT OUTER JOIN.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.
table2: Second table
matching_column: Column common to both the tables.
Note: We can also use LEFT OUTER JOIN instead of LEFT JOIN, both
are the same.
C. RIGHT JOIN
RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows of the
table on the right side of the join and matching rows for the table on the
left side of the join. For the rows for which there is no matching row on
the left side, the result-set will contain null. RIGHT JOIN is also known
as RIGHT OUTER JOIN.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
Note: We can also use RIGHT OUTER JOIN instead of RIGHT JOIN, both
are the same.
FROM Student
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Output:
D. FULL JOIN
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.
table2: Second table
matching_column: Column common to both the tables.
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Output:
NAME COURSE_ID
HARSH 1
PRATIK 2
RIYANKA 2
DEEP 3
SAPTARHI 1
DHANRAJ NULL
ROHIT NULL
NIRAJ NULL
NULL 4
NULL 5
NULL 4
CREATE-
There are two CREATE statements available in SQL:
CREATE DATABASE
CREATE TABLE
CREATE DATABASE
A Database is defined as a structured set of data. So, in SQL
the very first step to store the data in a well structured manner
is to create a database. The CREATE DATABASE statement is
used to create a new database in SQL.
Syntax:
CREATE DATABASE database_name;
database_name: name of the database.
Example Query: This query will create a new database in SQL
and name the database as my_database.
CREATE TABLE
We have learned above about creating databases. Now to
store the data we need a table to do that. The CREATE TABLE
statement is used to create a table in SQL. We know that a
table comprises of rows and columns. So while creating tables
we have to provide all the information to SQL about the names
of the columns, type of data to be stored in columns, size of
the data etc. Let us now dive into details on how to use
CREATE TABLE statement to create tables in SQL.
Syntax:
(
column1 data_type(size),
column2 data_type(size),
column3 data_type(size),
....
);
table_name: name of the table.
column1 name of the first column.
data_type: Type of data we want to store in the par
ticular column.
For example,int for integer data.
size: Size of the data we can store in a particular
column. For example if for
a column we specify the data_type as int and size a
s 10 then this column can store an integer
number of maximum 10 digits.
Example Query: This query will create a table named
Students with three columns, ROLL_NO, NAME and SUBJECT.
CREATE TABLE Students
(
ROLL_NO int(3),
NAME varchar(20),
SUBJECT varchar(20),);
HIMANSHU KUMAR(LINKEDIN)
https://www.linkedin.com/in/himanshukumarmahuri
CREDITS- INTERNET
DISCLOSURE- THE DATA AND IMAGES ARE TAKEN FROM GOOGLE AND INTERNET.
𝑪𝑯𝑬𝑪𝑲𝑶𝑼𝑻 𝑨𝑵𝑫 𝑫𝑶𝑾𝑵𝑳𝑶𝑨𝑫 𝑴𝒀 𝑨𝑳𝑳 𝑵𝑶𝑻𝑬𝑺
𝑳𝑰𝑵𝑲- https://linktr.ee/exclusive_notes