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

12 I.P. SQL(Complete)

The document provides an overview of MySQL functions, categorizing them into single row functions, multiple row functions, string functions, numeric functions, and date/time functions. It explains various functions with examples, such as CHAR, CONCAT, LOWER, and aggregate functions like AVG and COUNT. Additionally, it covers the use of the ORDER BY clause for sorting query results in SQL.

Uploaded by

mahekmadhan789
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

12 I.P. SQL(Complete)

The document provides an overview of MySQL functions, categorizing them into single row functions, multiple row functions, string functions, numeric functions, and date/time functions. It explains various functions with examples, such as CHAR, CONCAT, LOWER, and aggregate functions like AVG and COUNT. Additionally, it covers the use of the ORDER BY clause for sorting query results in SQL.

Uploaded by

mahekmadhan789
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Class – XII

Informatics Practices (I.P.)


Unit – 2
“Database Query using SQL”
MySQL Functions:
Function is a special type of predefined command set that performs some operation and
returns a single value. The value that are provided to functions are called parameters or
arguments.
Functions in MySQL can be either single row functions or multiple row functions:
Single Row Functions: Work on each individual row of a table and return result for each
row. For example, ucase(), round(), math() etc. are single row functions.
Multiple Row (Aggregate) Functions: Work on multiple rows together and return a
summary result for a group of rows. For example, sum(), avg(), count(), max(), min(), etc.
are multiple row functions.
Text/String Functions:
The string functions of MySQL can manipulate the text string in many ways. Some
commonly used string functions are as follows:
1. CHAR( ): This function returns the character for each integer passed.
CHAR(value1, value2, ..)
o Returns a string made up of the ASCII representation of the decimal value list.
o Strings in numeric format are converted to a decimal value.
o Null values are ignored.
Argument types: Integers Return Value: String
Example:
mysql>SELECT CHAR(70, 65, 67, 69); mysql>SELECT CHAR(65, 67.3, 69.3);
FACE ACE
2. CONCAT( ): This function concatenates two strings.
CONCAT(str1, str2)
o Returns argument str1 concatenated with argument str2.
Argument Types: String, String Return Value: String
mysql>SELECT CONCAT(“Python”, “Programming”);
PythonProgramming

Page 1 of 16
3. LOWER/LCASE: This function conver4ts a string into lowercase.
Synatx: LOWER(str) LCASE(str)
Argument Type: String Return Type: String
o Returns the argument str, with all letters in lowercase.
o The return value has the same data type as the argument char (CHAR or
VARCHAR)
mysql>SELECT LOWER(‘MR.OBAMA’); SELECT LCASE(‘Mr. Gandhi’);;
4. SUBSTR: This function extracts a substring from a given string.
SUBSTR(str, m[, n])
Argument Type: String, Numeric[, Numeric] Return Type: String
o Floating point number will be converted as integer.
o m is 0, it is treated as 1, if n is less than 1, a null is returned.
mysql>SELECT SUBSTR(‘ABCDEFG’, 3, 4);
CDEF
5. UPPER/UCASE: This function converts the given string into upper case.
UPPER(str) UCASE(str)
Argument Type: String Return Value: String
o Returns argument str, with all letters uppercase.
mysql> SELECT UPPER(‘Large’); OR SELECT UCASE(‘Large’);
LARGE
6. LTRIM: This function removes leading spaces i.e., spaces from the left of given
string.
LTRIM(str)
Argument Type: String Return Value: String
o Removes spaces from the left of argument str with initial characters removed.
mysql>SELECT LTRIM(‘ RDBMS MySQL’);
RDBMS MySQL
7. RTRIM: This function removes trailing spaces i.e., spaces from the right of given
string.
Argument Type: String Return Type: String
Returns str, with trailing spaces removed after the last character.
mysql>SELECT RTRIM (‘ RDBMS MySQL ’);

Page 2 of 16
RDBMS MySQL
#Note, that RTRIM() removes only the trailing spaces. Spaces in the middle or
leading spaces remain untouched with RTRIM().
8. TRIM: This function removes leading and trailing spaces from a given string. It
performs combined functions of LTRIM() and RTRIM().
TRIM(str)
TRIM([{BOTH| LEADING | TRAILING} [remstr] FROM] str)
mysql>SELECT TRIM(‘ Bar One ’);
Bar One
mysql>SELECT TRIM(leading ‘ ‘ FROM ‘ Bar One ‘);
Bar One
mysql>SELECT TRIM(leading ‘x’ FROM ‘xxxxBar Onexxxx’);
Bar One
9. INSTR: This function searches for given second string into the given first string.
INSTR(str1, str2)
Searches str1 for str2 and returns the position.
Argument Type: String Return Value: Number
mysql>SELECT INSTR(‘CORPORATE FLOOR’, ‘OR’);
2
10. LENGTH: This function returns the length of a given string in bytes.
LENGTH(str)
o Return the length of parameter specified by the argument str in characters.
o If argument str has datatype CHAR, the length includes all the trailing blanks.
o If argument str is null, this function returns null.
mysql>SELECT LENGTH(‘CANDIDE’);
7
11. LEFT: This function returns the leftmost number of characters as specified.
LEFT(str, len)
Argument Type: String, Integer Return Value: String
o Returns the leftmost len characters from the string str.
o Returns NULL if any argument is NULL.
mysql>SELECT LEFT(‘USS/23/67/09’, 3); USS

Page 3 of 16
12. RIGHT: This function returns the rightmost number of characters as specified.
RIGHT(str, len)
Argument Type: String, Integer, Return Value: String
o Returns the rightmost len characters from the string str.
o Returns NULL if any argument is NULL.
mysql>SELECT RIGHT(‘USS/23/67/09’, 2);
09
13. MID: This function returns a substring starting from the specified position.
MID (str, pos, len)
Argument Type: Integers Return Value: String
o Returns a substring from str starting from pos and having number of characters
as len.
o MID(str, pos, len) is a synonym for substring(str, pos, len)
mysql>SELECT SUBSTRING(‘Quadratically’, 5,6);
OR
mysql>SELECT MID(‘Quadratically’, 5,6);
ratica
Numeric Functions:
The number functions are those functions that accept numeric values and after
performing the required operation, return numeric values.
1. MOD: This function returns modules of given two numbers.
MOD(m, n), M % N, M MOD N
Returns remainder of argument m divided by argument n.
Returns m if n is 0 i.e., if denominator is 0.
Argument Type: Numeric, Numeric Return Value: Numeric
mysql> SELECT MOD(11,4); 3
2. POWER / POW: This function returns mn i.e., number m raised to the nth power.
POWER(m, n) POW(m, n)
Returns value of argument m raised to the nth power. The base m and the exponent n
can be any numbers, but if m is negative, n must be an integer.
Argument Type: Numeric, Numeric Return Value: Numeric
mysql> SELECT POWER(3, 2); 9

Page 4 of 16
3. ROUND: This function returns a number rounded off as per given specifications.
ROUND(n[, m])
Returns value of argument n rounded to m places right of the decimal point.
mysql> SELECT ROUND(15.193, 1); 15.2
4. SIGN: This function returns sign of a given number.
SIGN(n)
If argument n < 0, the function returns -1;
If argument n = 0, the function returns 0;
If argument n > 0, the function returns 1;
mysql> SELECT SIGN(-15); -1
5. SQRT: This function returns the square root of given number.
SQRT(n)
Returns square root of argument n. The value n cannit be negative.
SQRT returns a real result.
mysql> SELECT SQRT(26); 5.09901951
6. TRUNCATE: This function returns a number with some digits truncated.
TRUNCATE(n, m)
Returns value of argument n truncated to argument m decimal places;
If argument m is given as 0 places, m can be negative to truncate (i.e., make zero) m
digits left of the decimal point.
mysql> SELECT TRUNCATE(15.79, 1); 15.7
Date/Time Functions:
Data functions operate on valyes of the DATE datatype.
1. CURDATE() / CURRENT_DATE():
This function returns the current date.
CURDATE() OR CURENT_DATE() OR CURRENT_DATE
Returns the current date as a value in ‘YYYY-MM-DD’ or ‘YYYYMMDD’ format,
depending on whether the function is used in a string or numeric context.
Argument Type: None Return Value: Date
mysql>SELECT CURDATE(); 2023-04-03
mysql>SELECT CURDATE() + 10; 2023-04-13

Page 5 of 16
2. DATE(): This functions extracts the date part of a date or datetime expression.
DATE(expr)
Extracts the date part of the date or datetime expression expr
Argument Type: Date or Date time Return Value: date
mysql> SELECT DATE(‘2022-12-21 01:02:03’); 2022-12-21
3. MONTH(): This function returns the month from the data passed.
MONTH(date)
Returns the month for date, in the range 1 to 12 for January to December.
Returns 0 for dates such as ‘0000-00-00’ or ‘2022-00-00’ that have a zero month part.
Argument Type: date Return Value: Integer
4. MONTHNAME(): This function returns the name of the month for a date.
MONTHNAME(date)
Returns the name of the month of the given date.
mysql>SELECT MONTHNAME(‘2020-05-01’); MAY
5. DAY(): This function returns the day part of a date.
DAY(date)
Returns the day part of the given date.
Argument Types: date Return Value: Integer
mysql> SELECT DAY(‘2020-04-13’); 13
6. YEAR(): This function returns the year part of a date.
YEAR(date)
Returns the year for date, in the range 1000 to 9999.
Returns 0 for the “zero” date.
Argument Type: date Return Value: Integer
7. DAYNAME(): This function returns the name of weekday.
DAYNAME(date)
Returns the name of the weekday for date.
Argument Types: date Return Value: String
mysql>SELECT DAYNAME(‘2023-02-03’); Tuesday

Page 6 of 16
8. DAYOFMONTH(): This function returns the day of month.
DAYOFMONTH(date)
 Returns the day of the month for date in the range 1 to 31.
 Returns or 0 for dates such as ‘0000-00-00’ or ‘2008-00-00’ that have a zero
day part.
Argument Type: date Return Value: Integer
mysql>SELECT DAYOFMONTH(‘2023-02-03’); 3
9. DAYOFWEEK(): This function returns the day of week.
DAYOFWEEK(date)
 Returns the weekday index for date(1 = Sunday, 2 = Monday, …, 7 = Saturday).
Argument Type: date Return Value: Integer
mysql>SELECT DAYOFWEEK(‘2009-02-13’); 6
10. DAYOFYEAR(): This function returns the day of the year.
DAYOFYEAR(date)
 Returns the day of the year for date, in the range 1 to 366.
Argument Type: date Return Value: Integer
mysql>SELECT DAYOFYEAR(‘2009-02-13’);
11. NOW(): This function returns the current date and time.
 Returns the current date and time as a value in ‘YYYY-MM-DD HH:MM:SS’ or
YYYYMMDDHHMMSS.uuuuuu format, depending on the whether the function
is used in string or numeric context.
 The value is expressed in the current time zone.
Now() returns a constant time that indicates the time at which the statement began to
execute.
Argument Type: none Return Value: datetime
mysql> SELECT NOW(); 2023-09-15 13:36:57
12. SYSDATE(): This function returns the time at which the function executes.
SYSDATE( )
 Returns the current date and time as a value in ‘YYYY-MM-DD HH:MM:SS’
or YYYYMMDDHHMMSS.uuuuuu format, depending on the whether the
function is used in string or numeric context.

Page 7 of 16
 SYSDATE() returns the time at which it executes. This differs from the
behaviour for NOW(), which returns a constant time that indicates the time at
which the statement began to execute.
Argument Types: none Return Value: datetime
MySQL Functions Completed (Chapter – 6)

Querying Using SQL: The result set generated by the SQL SELECT statement is not
ordered in any form by default. However, if we want to sort or order the result set, we can
use the ORDER BY clause of SQL SELECT statement as per the following format:
SELECT <comma separated select list> FROM <table> [WHERE <condition>]
ORDER BY <fieldname> [ASC|DESC] [, <fieldname> [ASC|DESC], …];
Keywords ASC and DESC denote the order – Ascending and Descending. If we do not
specify any order keyword ASC or DESC, then by default, the ORDER BY clause sorts the
result set in ascending order.
Example: SELECT * FROM data ORDER BY marks ASC;
Ordering Data on Multiple Columns:
To order the result set on multiple columns, we can specify the multiple column names in
ORDER by clause along with the desired sort order, i.e., as:
SELECT
:
ORDER BY <filedname1> [ASC|DESC] [, <filedname1> [ASC|DESC], …];
mysql> SELECT * FROM data ORDER BY selection ASC, marks DESC;
Ordering Data on the Basis of an Expression:
Sometimes, we need to display the result of a calculation or a mathematical expression in the
result set. In such cases, we may want or need to arrange our result set in the order of the
calculated expression.
mysql>SELECT rollno, name, grade, section, marks * 0.35 FROM data WHERE marks > 70
ORDER BY section ASC, marks * 0.35 DESC;
If we want to procide a column alias name to the mathematical expression in the select list,
e.g., following statement will also produce the same result as above.
mysql>SELECT rollno, name, grade, section, marks * 0.35 as ‘Term1’ FROM data WHERE
marks > 70
ORDER BY section ASC, Term1 DESC;

Page 8 of 16
Aggregate Functions: MySQL also supports and provides group functions or aggregate
functions.
Many group functions accept the following options:
DISTINCT This option causes a group function to consider only distinct values of the
argument expression.
ALL This option causes a group function to consider all values including all duplicates.
1. AVG: This function computes the average of given data.
AVG([DISTINCT | ALL]n)
Returns average value of parameters of n.
Argument Type: Numeric Return Value: Numeric
mysql> SELECT AVG(Salary) FROM employee;
2. COUNT: This function counts the number of rows in a given column or expression.
Returns the number of rows in the query.
 If you specify argument expr, this function returns rows where expr is not null.
We can count either all rows, or only distinct values of expr.
 If we specify the asterisk (*), this function returns all rows, including duplicates
and nulls.
Argument Type: Numeric Return Value: Numeric
mysql> SELECT COUNT(*) FROM employee;
#It will return total number of records from table employee.
mysql> SELECT COUNT(job) FROM employee;
#It will count number of jobs in table employee.
mysql> SELECT COUNT(DISTINCT job) FROM employee;
#It will return number of distinct jobs that are listed in table employee;
3. MAX: This function returns the maximum value from a given column or expression.
MAX([DISTINCT|ALL] expr)
Returns maximum value of argument expr.
Argument Type: Numeric Return Value: Numeric
mysql> SELECT MAX(salary) FROM employee;
#It will display maximum salary from table employee.

Page 9 of 16
4. MIN: This function returns the minimum value from a given column or expression.
MIN([DISTINCT|ALL] expr)
Returns minimum value of argument expr.
Argument Type: Numeric Return Value: Numeric
mysql> SELECT MIN(salary) FROM employee;
#It will display minimum salary from table employee.
5. SUM: This function returns the sum of value in a given column or expression.
SUM([DISTINCT|ALL] n)
Returns sum of values of n.
Argument Type: Numeric Return Value: Numeric
mysql> SELECT SUM(salary) FROM employee;
#It will display total salary of all employees listed.
Examples:
1. To calculate the total gross for employees of grade ‘E2’, the command is:
SELECT SUM(gross) FROM employee WHERE grade=’E2’;
2. To display the average gross of employees with grades ‘E1’ or ‘E2’, the command
used is:
SELECT AVG(gross) FROM employee WHERE(grade=’E1’ or grade=’E2’);
3. To count the number of employees in employee table, the SQL command is:
SELECT COUNT(*) FROM employee;
4. To count the number of cities, the different members belong to you, use the
following command:
SELECT COUNT(DISTINCT city) FROM members;
Types of SQL Functions:
SQL supports many and many functions. All these functions can be generally categorized
into following two types:
Single Row (or Scaler) Functions.
Multiple Row (or Group or Aggregate) Functions.
(i) Single Row Functions work with a single row at a time. A single row function
returns a result for every row of a queried table.
(ii) Multiple Row or Group Functions work with data of multiple rows at a time and
return aggregated value.

Page 10 of 16
The difference between these two types of functions is in the number of rows they act upon.
A single row function works with the data of a single row at a time and returns a single
result for each row queried upon; a multiple row function works with a group of rows and
returns a single result for that group.
Grouping Result – GROUP BY
The GROUP BY clause is used in SELECT statements to divide the table into groups.
Grouping can be done by column name, or with aggregate functions in which case the
aggregate produces a value for each group.
For example, to calculate the number of employees in each grade, we can use command:
SELECT job, COUNT(*) FROM empl GROUP BY job;
Nested Groups – Grouping on Multiple Columns:
With GROUP BY clause, we can create groups within groups. Such type of grouping is
called Nested grouping. This can be done by specifying in GROUP BY expression, where
the first field determines the highest group level, the second field determines the second
group level, and so on.
To group records job wise within Deptno wise,
SELECT Deptno, Job, COUNT(empno) FROM empl GROUP BY Deptno, Job;
Placing Conditions on Groups – HAVING CLAUSE:
The HAVING clause places conditions on groups in contrast to WHERE clause that places
conditions on individual rows. While WHERE conditions cannot include aggregate
functions, HAVING conditions can do so.
For example, to calculate the average gross and total gross for employees belonging to ‘E4’
grade.
The command would be:
SELECT AVG(gross), SUM(gross)
FROM employee
GROUP BY grade
HAVING grade = ‘E4’;
To display the jobs where the number of employees in less than 3, we use the command:
SELECT job, COUNT(*) FROM empl GROUP BY job HAVING count(*) < 3;
The HAVING clause can contain either a simple Boolean expression (i.e., an expression or
condition that results into TRUE or FALSE) or use aggregate function in the having
condition.
We can include more than one condition in HAVING clause, by using logical operators.

Page 11 of 16
SELECT Deptno, AVG(Comm), AVG(Sal) FROM empl GROUP BY Deptno
HAVING AVG(Comm) > 750 AND AVG(Sal) > 2000;
Joins and Set Operations:
A join is a query that combines rows from two or more tables. In a join – query, more than
one table are listed in the FROM clause of SELECT query. The function of combining data
from multiple tables is called joining. The joining of two tables can be restricted (i.e., based
on some condition) or unrestricted (i.e., no binding condition).
Unrestricted Join (Cartesian Product):
When two tables are joined without any binding condition, such type of join is called
unrestricted join and the result produced is called the Cartesian Product. A Cartesian
Product of two tables consists of all possible combinations of rows coming from two tables:
SELECT * FROM employee, department;
Restricted Join (Join): In a SELECT query, fetching rows two tables, if we put a restriction
by adding a condition (WHERE clause) It will no longer be the Cartesian product.
It will now be called a restricted join or simply a join.
SELECT Ename, Location FROM employee, department WHERE employee.Age>12;
Q. Write query to join two tables employee and department on the basis of field deptno.
Ans. mysql> SELECT * FROM employee, department
-> WHERE employee.deptno = department.deptno;
Equi – Join:
In an equi – join, the values in the columns being joined are compared for equality. All the
columns in the tables-being-joined are included in the results.
Non – Equi Joins:
A non – equi – join is a query that specifies some relationship other than equality between
the columns.
Natural Join:
One of the two identical columns can be eliminated by restating the query. This result is
called a Natural Join (Equi – Join minus one of the two identical columns.)
For example,
SELECT employee.*, dname, loc
FROM employee, department
WHERE employee.deptno=department.deptno;

Page 12 of 16
Cross Join: The Cross join is a very basic type of join that simply matches each row from
one table to every row from another table. By default, it doesn’t apply any filtering condition
on the joined data. However, if we want to filter, we can use WHERE clause.
SELECT * FROM employee JOIN department ON (employee.deptno = department.deptno);
Natural Join:
Natural Join clause, we need not specify a JOIN – Condition. It will automatically create
natural – join through appropriate common field.
SELECT * FROM <table1> NATURAL JOIN <table2>;
SELECT * FROM employee NATURAL JOIN department;
Filtering Output:
If we want to further filter the records from a join – output, we can write the condition with a
WHERE clause.
mysql> SELECT * FROM employee NATURAL JOIN department WHERE salary>2000;
Left, Right Joins:
When we join tables based on some condition, we may find that only some, not all rows
from either table match with rows of other table.
Left Join: We can use LEFT JOIN clause of SELECT to produce left join i.e., as:
SELECT <select list>
FROM <table1> LEFT JOIN <table2> ON <joining-condition>;
When using LEFT JOIN “All rows from the first table will be returned” whether there are
matches in the second table or not. For unmatched rows of first table, NULL is shown in
columns of second table.
SELECT name, lastname, FROM members LEFT JOIN department on
members.id=department.id;
Right Join:
All rows from the second table are going to be returned whether or not there are matches in
the first table.
SELECT <select list>
FROM <table 1> RIGHT JOIN <table 2> ON <joining-condition>;
ORDERS ORDERITEMS
OrderID  OrderID 
OrderDate ProductID
CustomerID Quantity
TotalAmount

Page 13 of 16
CUSTOMERS PRODUCTS
CustomersID  ProductID 
Name ProductName
City SupplierID
Country UnitPrice
Phone

Q.1 Consider the Orders and Customers tables given above. Write an SQL query to list all
orders(OrderID) with customer information. (name, city and country).
Ans. SELECT OrderID, Name, City, Country FROM ORDERS, CUSTOMERS
WHERE ORDERS.OrderID = CUSTOMERS.CustomerID;
Q.2 Consider the Orders and Customers tables given above, Write an SQL Query to list all
customer details (name, phone) along with order date.
Ans. SELECT Name, Phone, Orderdate FROM ORDERS, CUSTOMERS
WHERE ORDERS.OrderID = CUSTOMERS.CustomerID;
Q.3 Consider the Orders and OrderItems tables given above, write an SQL Query to list
order details along with product IDs and Quantities.
Ans. SELECT ORDERS.OrderID, OrderDate, ProductID, Quantity FROM
ORDERS, CUSTOMERS
WHERE ORDERS.OrderID = CUSTOMERS.CustomerID;
Performing SET Operations on Relations:
Like joins, SQL set operations (UNION, INTERSECTION, MINUS) also combine the rows
coming from two or more relations (tables). But unlike SQL joins, which combine the two
relations on columns, SQL set operations combine the relations on rows.
UNION:
The SQL UNION operator allows manipulation of results returned by two or more queries
by combining the results of each query into a single result set.
Relation A Relation B
RollNo Name Age RollNo Name Age
101 Sidharth 19 301 Anubha 17
102 Kushagra 18 102 Kushagra 18
103 Usman 18 303 Sba 18

SELECT * FROM A UNION SELECT * FROM B;

Page 14 of 16
Result:
RollNo Name Age
101 Sidharth 19
102 Kushagra 18
103 Usman 18
301 Anubha 18
303 Sba 18
Rules of Using UNION Operator:
1. Both the SELECT statements must be UNION Compatible, that is, the select-lists of
SELECT statement must have same number of columns having similar data types and
order.

SELECT ecode, ename FROM branch1 INVALID


UNION
SELECT ename, ecode FROM branch2;

SELECT ecode, ename, sex FROM branch1 INVALID


UNION
SELECT ecode, ename, sex, grade FROM branch2;
2. The ORDER BY clause can occur only at the end of the UNION statement. It can’t be
used within the queries.
3. The ORDER BY and HAVING clause cannot be used to affect the final result.
Venue1 Venue2
Match_no City Match_no City
1 Delhi 1 Moahli
2 Mohali 2 Bengaluru
3 Kochi 3 Mumbai
4 Mumbai 4 Bengaluru
5 Bengaluru 5 Delhi
6 Chennai 6 Bengaluru
Q.1 Write an SQL query to union cities from tables venue1 and venue2.
Ans. SELECT City FROM Venue1
UNION [Delhi, Mohali, Kochi, Mumbai, Bengaluru, Chennai]
SELECT City FROM Venue2;
UNION vs UNION ALL
Both UNION and UNION ALL give the combine rows of two tables, but UNION removes
the duplicate entries while UNION ALL retains the duplicate entries.
Page 15 of 16
Query Query
Q1 Q2 1 2

MINUS Operator:
The MINUS operator is used to get the unique rows from the table1, i.e., the rows which are
in table1 but not in table2. MINUS is also known as EXCEPT.
With MINUS operator, we need to be careful with the order of SELECT statements as the
output will have only those rows that are in the first table and not in the second.

The MINUS operator is used as:


SELECT * FROM table1
MINUS
SELECT * FROM table2;
MySQL does not support MINUS operator directly but we can simulate MINUS operator
by using LEFT JOIN as per the following syntax:
SELECT <list> FROM <table1>
LEFT JOIN <table2>
ON <table1>.<common column> = <table2>.<common column>;
Q. Write an SQL query to get cities only in the table venue1 and not in table venue2.
Ans. SELECT City FROM Venue1 LEFT JOIN Venue2 ON Venue1.City = Venue2.City;
INTERSECT Operator:
In SQL, we can use an intersect operation (INTERSECT) to return rows that are common
between two tables. In other words, INTERSECT operation returns common rows from both
the left and right tables/queries.
<SELECT query1>
INTERSECT
<SELECT query2>;

Unit – 2 Database Query using SQL Completed…

Page 16 of 16

You might also like