0% found this document useful (0 votes)
25 views

Chapter 9

Uploaded by

clausn11223
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Chapter 9

Uploaded by

clausn11223
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 76

Chapter 9

SQL Concepts

Prepared By:
Ms. Ayushi Gondaliya
Assistant Prof.
SRCOE, RAJKOT
What Is SQL?
 SQL stands for Structured Query Language.
 It is used for storing and managing data in
relational database management system (RDMS).
 It is a standard language for Relational Database
System. It enables a user to create, read, update
and delete relational databases and tables
 All the RDBMS like MySQL, Informix, Oracle, MS
Access and SQL Server use SQL as their standard
database language.
 SQL allows users to query the database in a
number of ways, using English-like statements.
Rules:
 SQL follows the following rules:
1. Structure query language is not case
sensitive. Generally, keywords of SQL are
written in uppercase.
2. Statements of SQL are dependent on text
lines. We can use a single SQL statement
on one or multiple text line.
3. Using the SQL statements, you can
perform most of the actions in a database.
4. SQL depends on tuple relational calculus
and relational algebra.
SQL Data types:
 SQL Data type is used to define the values that
a column can contain.
 Every column is required to have a name and
data type in the database table.
 Data type of SQL:
1. Numeric data type
2. Exact numeric data type
3. String data type
4. Date data type
1. Numeric Data type:
 The subtypes of approximate numeric
data types are given below:
Data type From To Description

float -1.79E + 308 1.79E + 308 It is used to


specify a
floating-point
value e.g.
6.2, 2.9 etc.
real -3.40e + 38 3.40E + 38 It specifies a
single
precision
floating point
number
2. Exact numeric data type

Data type Description


int It is used to specify an integer value.

smallint It is used to specify small integer value.

bit It has the number of bits to store.

decimal It specifies a numeric value that can have a


decimal number.

numeric It is used to specify a numeric value.


3. character string data types:

Data type Description


char It has a maximum length of 8000 characters.
It contains Fixed-length non-unicode
characters.
varchar It has a maximum length of 8000 characters.
It contains variable-length non-unicode
characters.
text It has a maximum length of 2,147,483,647
characters. It contains variable-length non-
unicode characters.
4. Date and time data types:

Data type Description


date It is used to store the year, month, and days
value.
time It is used to store the hour, minute, and
second values.
timestamp It stores the year, month, day, hour, minute,
and the second value.
SQL Commands OR
Components of SQL
 SQL commands are instructions. It is used to
communicate with the database. It is also used
to perform specific tasks, functions, and queries
of data.
 SQL can perform various tasks like create a
table, add data to tables, drop the table, modify
the table, set permission for users.
Types Of Components/
commands
1. Data Definition Language (DDL)
 DDL changes the structure of the table like creating a table,
deleting a table, altering a table, etc.
1. CREATE It is used to create a new table in the database.
 Syntax:
◦ CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPE
S[,....]);
 Example:
◦ CREATETABLE EMPLOYEE(Name VARCHAR2(20), Email VARC
HAR2(100), DOB DATE);

2. DROP: It is used to delete both the structure and record


stored in the table.
 Syntax
◦ DROP TABLE ;
 Example:
◦ DROP TABLE EMPLOYEE;
 Create table student(id int(10), name
varchar2(20),email_add varchar2(50),
semester int(2),contact_no int(10));
3. ALTER: It is used to alter the structure of the database. This
change could be either to modify the characteristics of an
existing attribute or probably to add a new attribute.
 Syntax:
◦ To add a new column in the table
◦ ALTER TABLE table_name ADD column_name COLUMN-
definition;
◦ To modify existing column in the table:
◦ ALTER TABLE table_name
MODIFY(COLUMN DEFINITION....);
 EXAMPLE
◦ ALTER TABLE EMPLOYEE ADD(DEPARTMENT
VARCHAR2(20));
◦ ALTER TABLE EMPLOYEE MODIFY(Name VARCHAR2(30));
4. TRUNCATE: It is used to delete all the rows from the
table.
 Syntax:
◦ TRUNCATE TABLE table_name;
 Example:
◦ TRUNCATE TABLE EMPLOYEE;

5. RENAME: it is used to change the name of a table.


Sometimes, we choose non-meaningful name for the
table. So it is required to be changed.
 Syntax:
RENAME old_table _name To new_table_name;
 Example:
◦ RENAME EMPLOYEE TO EMP;
2. Data Manipulation Language
 DML commands are used to modify the database. It
is responsible for all form of changes in the
database.
 1. INSERT: The INSERT statement is a SQL query.
It is used to insert data into the row of a table.
 Syntax:
◦ INSERT INTO TABLE_NAME (col1, col2,.... colN) VALUES
(value1, value2, value3, .... valueN);
Or
◦ INSERT INTO TABLE_NAME VALUES (value1, value2, .... va
lueN);
 For example:
◦ INSERT INTO EMPLOYEE (Name, Email,
DOB) VALUES (“PRIYA",“abc@xyz.com",1/1/2000);
2.UPDATE: This command is used to update or modify
the value of a column in the table.
 Syntax:
◦ UPDATE table_name SET[column_name1=value1,...column_nam
eN = valueN] [WHERE CONDITION]
 For example:
◦ UPDATE EMPLOYEE SET Email = '‘xyz.1@abc.com” WHERE
Name = ”priya”;
 3. DELETE: It is used to remove one or more row
from a table.
 Syntax:
◦ DELETE FROM table_name [WHERE condition];
 For example:
◦ DELETE FROM EMPLOYE WHERE Name=“Priya";
3. Data Control Language
 DCL commands are used to grant and take
back authority from any database user.
 1. GRANT: It is used to give user access
privileges to a database.
 Example
◦ GRANT SELECT, UPDATE ON MY_TABLE TO SOM
E_USER,ANOTHER_USER;
 2. REVOKE: It is used to take back
permissions from the user.
 Example
◦ REVOKE SELECT, UPDATE ON MY_TABLE FROM U
SER1, USER2;
4.Transaction Control Language
 TCL commands can only use with DML commands
like INSERT, DELETE and UPDATE only.
 DDL operations are automatically committed in the
database that's why they cannot be used while
creating tables or dropping them.
 1. Commit: Commit command is used to save all
the transactions to the database.
 Syntax:
◦ COMMIT;
 Example:
◦ DELETE FROM CUSTOMERS WHERE AGE = 25;
◦ COMMIT;
 2. Rollback: Rollback command is used to
undo transactions that have not already been
saved to the database.
 Syntax:
◦ ROLLBACK;
 Example:
◦ DELETE FROM CUSTOMERS WHERE AGE = 25;
◦ ROLLBACK;
 3. SAVEPOINT: It is used to roll the
transaction back to a certain point without
rolling back the entire transaction.
 Syntax:
◦ SAVEPOINT SAVEPOINT_NAME;
5. Data Query Language
 DQL is used to fetch the data from the database.
 It is used to select the attribute based on the
condition described by WHERE clause.
 Syntax:
◦ SELECT expressions FROM TABLES WHERE con
ditions;
◦ SELECT * from Table name; (this query is used to
fetch all the data from that table)
 For example:
◦ SELECT ename FROM employee
WHERE age > 20;
◦ SELECT * from employee;
SQL Constraints
 SQL constraints are used to specify rules for the data in a table.
 Constraints can be column level or table level.
 The following constraints are commonly used in SQL:
 NOT NULL - Ensures that a column cannot have a NULL value
 UNIQUE - Ensures that all values in a column are different
 PRIMARY KEY - A combination of a NOT NULL and UNIQUE.
Uniquely identifies each row in a table
 FOREIGN KEY - Uniquely identifies a row/record in another
table
 CHECK - Ensures that all values in a column satisfies a specific
condition
 DEFAULT - Sets a default value for a column when no value is
specified
 INDEX - Used to create and retrieve data from the database very
quickly
SQL Aggregate Functions:
 SQL aggregation function is used to
perform the calculations on multiple rows
of a single column of a table. It returns a
single value.
 It is also used to summarize the data.
Types of Aggregate Functions:
Sample Table(PRODUCT_MAST)
PRODUCT COMPANY QTY RATE COST

Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item7 Com1 5 30 150
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Item10 Com3 4 30 120
 1. COUNT FUNCTION
◦ COUNT function is used to Count the
number of rows in a database table. It can
work on both numeric and non-numeric data
types.
◦ COUNT function uses the COUNT(*) that
returns the count of all the rows in a specified
table. COUNT(*) considers duplicate and
Null.
◦ Syntax
 COUNT(*) or
 COUNT( [ALL|DISTINCT] expression )
 Example: COUNT()
 SELECT COUNT(*) FROM PRODUCT_MAST;

 Output:
 10

 Example: COUNT with WHERE


 SELECT COUNT(*) FROM PRODUCT_MAST
WHERE RATE>=20;
 Output:
 7
 2. SUM Function
 Sum function is used to calculate the sum of
all selected columns. It works on numeric
fields only.
 Syntax
◦ SUM()
◦ or
◦ SUM( [ALL|DISTINCT] expression )
 Example: SUM()
◦ SELECT SUM(COST)
FROM PRODUCT_MAST;
 Output:
◦ 670
 3. AVG function
 The AVG function is used to calculate the
average value of the numeric type. AVG
function returns the average of all non-Null
values.
 Syntax
◦ AVG()
◦ or
◦ AVG( [ALL|DISTINCT] expression )
 Example:
◦ SELECT AVG(COST) FROM PRODUCT_MAST;
 Output:
◦ 67.00
 4. MAX Function
 MAX function is used to find the maximum
value of a certain column. This function
determines the largest value of all selected
values of a column.
 Syntax
◦ MAX()
◦ or
◦ MAX( [ALL|DISTINCT] expression )
 Example:
◦ SELECT MAX(RATE) FROM PRODUCT_MAST;
 Output:
◦ 30
 5. MIN Function
 MIN function is used to find the minimum
value of a certain column. This function
determines the smallest value of all selected
values of a column.
 Syntax
◦ MIN()
◦ or
◦ MIN( [ALL|DISTINCT] expression )
 Example:
◦ SELECT MIN(RATE) FROM PRODUCT_MAST;
 Output:
◦ 10
SQL Clause:
 1. GROUP BY:
 SQL GROUP BY statement is used to arrange
identical data into groups. The GROUP BY
statement is used with the SQL SELECT
statement.
 The GROUP BY statement is used with
aggregation function.
 Syntax
 SELECT column FROM table_name WHERE
conditions GROUP BY column;
Example:
 SELECT COMPANY, COUNT(*) FROM
PRODUCT_MAST GROUP BY COMPANY;
 Output:
◦ Com1 5
◦ Com2 3
◦ Com3 2
 2. HAVING
 HAVING clause is used to specify a search
condition for a group or an aggregate.
 Having is used in a GROUP BY clause. If you are
not using GROUP BY clause then you can use
HAVING function like a WHERE clause.
 Syntax:
 SELECT column1, column2
FROM table_name WHERE conditions GROUP
BY column1, column2
HAVING conditions ORDER BY column1, colu
mn2;
Example:
 SELECT COMPANY, COUNT(*) FROM PROD
UCT_MAST GROUP BY COMPANY HAVING
COUNT(*)>2;
 Output:
◦ Com1 5
◦ Com2 3
 3. ORDER BY
 The ORDER BY clause sorts the result-set in
ascending or descending order.
 It sorts the records in ascending order by default.
DESC keyword is used to sort the records in
descending order.
 Syntax:
 SELECT column1, column2 FROM
table_name WHERE condition
ORDER BY column1, column2...ASC|DESC;
 Where
◦ ASC: It is used to sort the result set in ascending order
by expression.
◦ DESC: It sorts the result set in descending order by
expression.
Example:
 Consider the table customer:
CUSTOMER_ID NAME ADDRESS

12 Kathrin US
23 David Bangkok
34 Alina Dubai
45 John UK
56 Harry US
 In Ascending Order:
 SELECT * FROM CUSTOMER ORDER BY
NAME;

 OUTPUT:
CUSTOMER_ID NAME ADDRESS

34 Alina Dubai
23 David Bangkok
56 Harry US
45 John UK
12 Kathrin US
 In Descending Order:
 SELECT * FROM CUSTOMER ORDER BY
NAME DESC;

 OUTPUT:
CUSTOMER_ID NAME ADDRESS

12 Kathrin US
45 John UK
56 Harry US
23 David Bangkok
34 Alina Dubai
SQL LIKE Operator
 The LIKE operator is used in a WHERE clause to search
for a specified pattern in a column.
 There are two wildcards often used in conjunction with
the LIKE operator:
◦ % - The percent sign represents zero, one, or
multiple characters
◦ _ - The underscore represents a single
character
 LIKE Syntax:
 SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
 Select emp_name , salary from employee where
emp_name like ‘v_i%i’;
 Here are some examples showing different LIKE
operators with '%' and '_' wildcards:
LIKE Operator Description
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the
second position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and
are at least 2 characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and
are at least 3 characters in length
WHERE CustomerName LIKE 'a%o' Finds any values that start with "a" and
ends with "o"
SQL IN Operator
 The IN operator allows you to specify
multiple values in a WHERE clause.
 The IN operator is a shorthand for
multiple OR conditions.
 IN Syntax:
◦ SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
Example
 SELECT * FROM Customers
WHERE ADDRESS IN (‘US', 'UK');
CUSTOMER_ID NAME ADDRESS

12 Kathrin US
45 John UK
56 Harry US

 SELECT * FROM Customers


WHERE Country NOT IN (‘US', 'UK');
CUSTOMER_ID NAME ADDRESS

23 David Bangkok
34 Alina Dubai
SQL Built-In Functions:
 1. Numeric Functions
 2. Date Functions
 3. String Functions
1. Numeric Functions:
Function Description Example
ABS() It returns the absolute value SELECT ABS(-243.5);
of a number. Output: 243.5
CEIL() It returns the smallest SELECT CEIL(25.75);
integer value that is greater Output: 26
than or equal to a number.
DIV() It is used for integer SELECT 10 DIV 5;
division. Output: 2
EXP() It returns e raised to the SELECT EXP(1);
power of number. Output: 2.718281
FLOOR() It returns the largest integer SELECT FLOOR(25.75);
value that is less than or Output: 25
equal to a number.
GREATEST() It returns the greatest value SELECT GREATEST(30, 2, 36,
in a list of expressions. 81, 125);
Output: 125
Function Description Example
LEAST() It returns the smallest SELECT LEAST(30, 2, 36,
value in a list of 81, 125);
expressions. Output: 2

LN() It returns the natural SELECT LN(2);


logarithm of a number. Output: 0.693147
LOG10() It returns the base-10 SELECT LOG(2);
logarithm of a number. Output: 0.693147

MOD() It returns the remainder SELECT MOD(18, 4);


of n divided by m. Output: 2

PI() It returns the value of PI SELECT PI();


displayed with 6 decimal Output: 3.141593
places.
POW() It returns m raised to the SELECT POW(4, 2);
nth power. Output: 16
Function Description Example

ROUND() It returns a number rounded to a SELECT


certain number of decimal places. ROUND(5.553);
Output: 6

SIGN() It returns a value indicating the sign SELECT SIGN(255.5);


of a number. Output: 1

SQRT() It returns the square root of a SELECT SQRT(25);


number. Output: 5
TRUNCAT It returns 7.53635 truncated to 2 SELECT
E() places right of the decimal point. TRUNCATE(7.53635, 2);
Output: 7.53
2. Date Functions
Function Description Example
NOW() Returns the current date SELECT NOW();
and time.
CURDATE() Returns the current SELECT CURDATE();
date.
CURTIME() Returns the current SELECT CURTIME();
time.
DATE() Extract the date part of SELECT DATE('2020-12-
a date or datetime 31 01:02:03');
expression Output: 2020-12-31
DATEDIFF() Returns the number of SELECT
days between two dates. DATEDIFF('2017-01-
13','2017-01-03') AS
DateDiff;
Output: 10
DATE_FORMAT() Displays date/time data DATE_FORMAT(NOW(),'%
in different formats d %b %y')
Output: 13 Jan 17
3. String Functions
Function Description Example
ASCII() This function is used to SELECT ascii('t');
find the ASCII value of a Output: 116
character.
LEN() This function is used to find SELECT len('Hello!');
the length of a word. Output: 6
CONCAT() This function is used to add SELECT CONCAT('My',
two words or strings. 'S', 'QL');
Output: MySQL
INSERT(str, pos, len, This function is used to SELECT
newstr) insert the sub string. INSERT('Quadratic', 3, 4,
'What');
Output: QuWhattic
INSTR() This function is used to SELECT
find the occurrence of INSTR('foobarbar', 'bar');
string. Output: 4
Function Description Example
LCASE() This function is used to convert LCASE(“DBMS”);
the given string into lower case. Output: dbms
LEFT(str,len) Returns the leftmost len SELECT
characters from the string str. LEFT('foobarbar', 5);
Output: fooba
LTRIM(str) Returns the string str with SELECT LTRIM('
leading space characters barbar');
removed. Output: barbar
LPAD(str,len,pads This function is used to make SELECT LPAD('hi',4,'??');
tr) the given string of the given size Output: ??hi
by adding the given symbol.
REPEAT() This function is used to write SELECT REPEAT(‘abc',
the given string again and again 2);
till the number of times Output: abcabc
mentioned.
REPLACE(str,fr Returns the string str with all SELECT REPLACE
om_str,to_str occurrences of the string (‘aww.mysql.com', ‘a',
) from_str replaced by the string 'w');
to_str Output: www.mysql.com
Function Description Example
REVERSE(str) Returns the string str SELECT
with the order of the REVERSE('abcd');
characters reversed. Output: dcba
RIGHT(str,len) Returns the rightmost SELECT
len characters from the RIGHT('foobarbar', 4);
string str. Output: rbar
STRCMP(str1, str2) Compares two strings SELECT
and returns 0 if both STRCMP('MOHD',
strings are equal, it 'MOHD');
returns -1 if the first Output: 0
argument is smaller than
the second according to
the current sort order
otherwise it returns 1.
UCASE(str) This function is used to LCASE(“dbms”);
convert the given string Output: DBMS
into upper case.
Set Operations
 The SQL Set operation is used to combine the
two or more SQL SELECT statements.
 Types of Set Operation
1. Union
2. Intersect
3. Minus
 1. Union
 The SQL Union operation is used to combine
the result of two or more SQL SELECT
queries.
 In the union operation, all the number of
datatype and columns must be same in both the
tables on which UNION operation is being
applied.
 The union operation eliminates the duplicate
rows from its result set.
 Syntax
◦ SELECT column_name FROM table1 UNION
SELECT column_name FROM table2;
Example:
First Second
ID NAME ID NAME

1 Jack 3 Jackson
2 Harry 4 Stephan
3 Jackson 5 David

SELECT * FROM First UNION SELECT * FROM Second;

ID NAME

1 Jack
2 Harry
3 Jackson
4 Stephan
5 David
 2. Intersect
 The Intersect operation returns the common
rows from both the SELECT statements.
 In the Intersect operation, the number of
datatype and columns must be the same.
 It has no duplicates and it arranges the data in
ascending order by default.

 Syntax
◦ SELECT column_name FROM table1INTERSECT
SELECT column_name FROM table2;
Example:
 Using the above First and Second table.
 Intersect query will be:
◦ SELECT * FROM First INTERSECT
SELECT * FROM Second;
 Output:

ID NAME

3 Jackson
 3. Minus
 It combines the result of two SELECT
statements. Minus operator is used to display
the rows which are present in the first
query but absent in the second query.
 It has no duplicates and data arranged in
ascending order by default.

 Syntax:
◦ SELECT column_name FROM table1 MINUS
SELECT column_name FROM table2;
Example:
 Using the above First and Second table.
 Minus query will be:
◦ SELECT * FROM First MINUS
SELECT * FROM Second;

 Output:

ID NAME

1 Jack
2 Harry
SQL Sub Query
 A Sub-query is a query within another SQL query and
embedded within the WHERE clause.

 Important Rule:
 A sub-query can be placed in a number of SQL clauses like
WHERE clause, FROM clause, HAVING clause.
 You can use Sub-query with SELECT, UPDATE, INSERT,
DELETE statements along with the operators like =, <, >, >=,
<=, IN, BETWEEN, etc.
 A sub-query is a query within another query. The outer
query is known as the main query, and the inner query is
known as a sub-query.
 Sub-queries are on the right side of the comparison
operator.
 A sub-query is enclosed in parentheses.
 In the Sub-query, ORDER BY command cannot be used.
1. Subqueries with the Select
Statement
 SQL subqueries are most frequently used with
the Select statement.
 Syntax:
◦ SELECTcolumn_name FROM table_name
WHERE column_name expression operator
( SELECT column_name from table_name
WHERE ... );
Consider the EMPLOYEE table:

ID NAME AGE ADDRESS SALARY

1 John 20 US 2000.00

2 Stephan 26 Dubai 1500.00

3 David 27 Bangkok 2000.00

4 Alina 29 UK 6500.00

5 Kathrin 34 Bangalore 8500.00

6 Harry 42 China 4500.00

7 Jackson 25 Mizoram 10000.00


 Give the details of employees whoes salary is
grater than 4500.
◦ SELECT * FROM EMPLOYEE WHERE ID IN (SELECT
ID FROM EMPLOYEE
WHERE SALARY > 4500);
 Output:
ID NAME AGE ADDRESS SALARY

4 Alina 29 UK 6500.00

5 Kathrin 34 Bangalore 8500.00

7 Jackson 25 Mizoram 10000.00


2. Subqueries with the INSERT
Statement
 SQL subquery can also be used with the Insert
statement. In the insert statement, data
returned from the subquery is used to insert
into another table.
 Syntax:
◦ INSERT INTO table_name (column1, column2, colum
n3....) SELECT * FROM table_name
WHERE VALUE OPERATOR
 Example
 Consider a table EMPLOYEE_BKP with similar
as EMPLOYEE.
 So, copy the complete EMPLOYEE table into
the EMPLOYEE_BKP table.
◦ INSERT INTO EMPLOYEE_BKP SELECT * FROM E
MPLOYEE WHERE ID IN (SELECT ID FROM EMP
LOYEE);
3. Subqueries with the UPDATE
Statement
 The subquery of SQL can be used in
conjunction with the Update statement.
 When a subquery is used with the Update
statement, then either single or multiple
columns in a table can be updated.
 Syntax:
 UPDATE table
◦ SET column_name = new_value WHERE VALUE
OPERATOR (SELECT COLUMN_NAME FROM
TABLE_NAME WHERE condition);
 Example:
 The given example updates the SALARY by .25
times in the EMPLOYEE table for all employee
whose AGE is greater than or equal to 29.
◦ UPDATE EMPLOYEE SET SALARY = SALARY * 0.25
WHERE AGE IN (SELECT AGE FROM
CUSTOMERS_BKP WHERE AGE >= 29)
 Output:
ID NAME AGE ADDRESS SALARY

1 John 20 US 2000.00
2 Stephan 26 Dubai 1500.00
3 David 27 Bangkok 2000.00
7 Jackson 25 Mizoram 10000.00
SQL JOIN
 As the name shows, JOIN means to combine
something. In case of SQL, JOIN means "to
combine two or more tables".
 In SQL, JOIN clause is used to combine the
records from two or more tables in a database.
 Types of SQL JOIN
1. INNER JOIN
2. LEFT JOIN
3. RIGHT JOIN
4. FULL JOIN
Consider the sample table Employee and
Project:
EMP_ID EMP_NAME CITY SALARY AGE

1 Angelina Chicago 200000 30


2 Robert Austin 300000 26
3 Christian Denver 100000 42
4 Kristen Washington 500000 29
5 Russell Los angels 200000 36
6 Marry Canada 600000 48

PROJECT_NO EMP_ID DEPARTMENT

101 1 Testing
102 2 Development
103 3 Designing
104 4 Development
1. INNER JOIN
 INNER JOIN selects records that have matching
values in both tables as long as the condition is
satisfied. It returns the combination of all rows
from both the tables where the condition
satisfies.
 Query
◦ SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT FRO
M EMPLOYEE INNER JOIN PROJECT ON PROJECT.EMP_ID =
EMPLOYEE.EMP_ID;
 Output: EMP_NAME DEPARTMENT

Angelina Testing
Robert Development
Christian Designing
Kristen Development
2. LEFT JOIN
 The SQL left join returns all the values from left table
and the matching values from the right table. If there is
no matching join value, it will return NULL.
 Query:
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
FROM EMPLOYEE LEFT JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
 O/p: EMP_NAME DEPARTMENT

Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
3. RIGHT JOIN
 In SQL, RIGHT JOIN returns all the values from the
values from the rows of right table and the matched
values from the left table. If there is no matching in both
tables, it will return NULL.
 Query:
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT FROM
EMPLOYEE RIGHT JOIN PROJECT ON PROJECT.EMP_ID = EMP
LOYEE.EMP_ID;
 O/p:

EMP_NAME DEPARTMENT

Angelina Testing
Robert Development
Christian Designing
Kristen Development
4. FULL JOIN
 In SQL, FULL JOIN is the result of a combination of
both left and right outer join. Join tables have all the
records from both tables. It puts NULL on the place of
matches not found.
 Query:
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT FROM
EMPLOYEE FULL JOIN PROJECT ON PROJECT.EMP_ID = EMPL
OYEE.EMP_ID;
 O/P: EMP_NAME DEPARTMENT

Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
Views in SQL
 Views in SQL are considered as a virtual table.
A view also contains rows and columns.
 To create the view, we can select the fields from
one or more tables present in the database.
 A view can either have specific rows based on
certain condition or all the rows of a table.
How to create view:
 A view can be created using the CREATE
VIEW statement.
 Syntax:
CREATE VIEW view_name AS SELECT
column1, column2..... FROM table_name
WHERE condition;
Query:
 CREATE VIEW DetailsView AS SELECT NAME,
ADDRESS FROM Student_Details
WHERE STU_ID < 4;
 Just like table query, we can query the
view to view the data.
◦ SELECT * FROM DetailsView;
 Output:
NAME ADDRESS

Stephan Delhi
Kathrin Noida
David Ghaziabad
Thank YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy