SQL Most Important Concepts Part-3
SQL Most Important Concepts Part-3
SQL
MOST IMPORTANT CONCEPTS
PLACEMENT PREPARATION
[EXCLUSIVE NOTES]
TOPICS COVERED-
PART-3 :-
➢ AND and OR operators
➢ Union Clause
➢ Join (Cartesian Join & Self Join)
➢ DROP, DELETE, TRUNCATE
➢ DROP, TRUNCATE
➢ Date functions
➢ EXISTS
➢ WITH clause
➢ NULL Values
HIMANSHU KUMAR(LINKEDIN)
https://www.linkedin.com/in/himanshukumarmahuri
AND Operator:-
Syntax:
SELECT * FROM table_name WHERE condition1 AND condi
tion2 and ...conditionN;
Take another example, to fetch all the records from the Student
table where NAME is Ram and Age is 18.
Query:
To fetch all the records from the Student table where NAME is
Ram or NAME is SUJIT.
Query:
SELECT * FROM Student WHERE NAME = 'Ram' OR NAME =
'SUJIT';
Output:
To fetch all the records from the Student table where NAME is
Ram or Age is 20.
Query:
Syntax:
SELECT * FROM table_name WHERE condition1 AND (cond
ition2 OR condition3);
Take an example to fetch all the records from the Student
table where Age is 18 NAME is Ram or RAMESH.
Query:
SELECT * FROM Student WHERE Age = 18 AND (NAME = 'R
am' OR NAME = 'RAMESH');
Union Clause-
The Union Clause is used to combine two separate select
statements and produce the result set as a union of both the
select statements.
NOTE:
Basic Syntax:-
Output:
ROLL_NO
Output:
ROLL_NO
Output:
ROLL_NO NAME
1 Information Technology
2 Computer Science
4 SURESH
• CARTESIAN JOIN
• SELF JOIN
StudentCourse
Syntax:
Syntax:
Output:
DROP vs TRUNCATE
DROP, TRUNCATE-
DROP
DROP is used to delete a whole database or just a table.The
DROP statement destroys the objects like an existing
database, table, index, or view.
A DROP statement in SQL removes a component from a
relational database management system (RDBMS).
TRUNCATE
TRUNCATE statement is a Data Definition Language (DDL)
operation that is used to mark the extents of a table for
deallocation (empty for reuse). The result of this operation
quickly removes all data from a table, typically bypassing a
number of integrity enforcing mechanisms.
The TRUNCATE TABLE mytable statement is logically (though
not physically) equivalent to the DELETE FROM mytable
statement (without a WHERE clause).
Syntax:
TRUNCATE TABLE table_name;
table_name: Name of the table to be truncated.
DATABASE name - student_data
Queries
Date functions-
In SQL, dates are complicated for newbies, since while
working with database, the format of the date in table must be
matched with the input date in order to insert. In various
scenarios instead of date, datetime (time is also involved with
date) is used.
In MySql the default date functions are:
• SELECT NOW();
Output:
2017-01-13 08:03:52
• SELECT CURDATE();
2017-01-13
• SELECT CURTIME();
Output:
08:05:15
Id Name BirthTime
Name BirthDate
Pratik 1996-09-26
There are several units that can be considered but only some
are used such as:
Example:
For the below table named 'Test'
Id Name BirthTime
Queries
SELECT Name, Extract(DAY FROM BirthTime) AS BirthDa
y FROM Test;
Output:
Name BirthDay
Pratik 26
SELECT Name, Extract(YEAR FROM BirthTime) AS BirthY
ear FROM Test;
Output:
Name BirthYear
Pratik 1996
SELECT Name, Extract(SECOND FROM BirthTime) AS Birt
hSecond FROM Test;
Name BirthSecond
Pratik 581
Syntax:
Example:
For the below table named 'Test'
Id Name BirthTime
Queries
o SELECT Name, DATE_ADD(BirthTime, INTERVAL 1
YEAR) AS BirthTimeModified FROM Test;
Name BirthTimeModified
Output:
Name BirthDayModified
Output:
Name BirthSecond
• DATEDIFF(date1, date2);
• date1 & date2- date/time expression
Output:
DateDiff
10
• DATE_FORMAT(date,format);
date is a valid date and format specifies the output format for
the date/time. The formats that can be used are:
Example:
DATE_FORMAT(NOW(),'%d %b %y')
Result:
13 Jan 17
Syntax:
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name(s)
FROM table_name
WHERE condition);
Examples:
Consider the following two relation "Customers" and "Orders".
Output:
2. Using NOT with EXISTS Fetch last and first name of the
customers who has not placed any order.
DELETE
FROM Orders
WHERE EXISTS (SELECT *FROM customers WHERE Custom
ers.customer_id = Orders.cid AND Customers.lname
= 'Mehra'); SELECT * FROM Orders;
Output:
UPDATE Customers
SET lname = 'Kumari'
WHERE EXISTS (SELECT *FROM Customers WHERE custom
er_id = 401);
SELECT * FROM Customers;
WITH clause-
The SQL WITH clause was introduced by Oracle in the Oracle
9i release 2 database. The SQL WITH clause allows you to
give a sub-query block a name (a process also called sub-
query refactoring), which can be referenced in several places
within the main SQL query.
Syntax:
SQL Query:
WITH temporaryTable(averageValue) as
(SELECT avg(Salary)
from Employee)
SELECT EmployeeID,Name, Salary
FROM Employee, temporaryTable
WHERE Employee.Salary > temporaryTable.aver
ageValue;
Example 2: Find all the airlines where the total salary of all
pilots in that airline is more than the average of total salary of
all pilots in the database.
Airline
Airbus 380
NULL Values-
In SQL there may be some records in a table that do not have
values or data for every field. This could be possible because at
a time of data entry information is not available. So SQL
supports a special value known as NULL which is used to
represent the values of attributes that may be unknown or not
apply to a tuple. SQL places a NULL value in the field in the
absence of a user-defined value. For example, the
Apartment_number attribute of an address applies only to
address that are in apartment buildings and not to other types
of residences.
Importance of NULL value:
Output:
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