0% found this document useful (0 votes)
1 views8 pages

SQL Remaining Notes

The document provides a comprehensive overview of SQL concepts including NULL values, update and delete statements, aggregate functions, and various types of joins such as Cartesian and equi-joins. It also includes examples of SQL queries for different scenarios involving tables like SCHOOL, ADMIN, TRANSPORT, and COMPANY. Additionally, it covers the use of SQL commands for data retrieval and manipulation, along with specific queries and their expected outputs.

Uploaded by

Neeraj Tiwari
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)
1 views8 pages

SQL Remaining Notes

The document provides a comprehensive overview of SQL concepts including NULL values, update and delete statements, aggregate functions, and various types of joins such as Cartesian and equi-joins. It also includes examples of SQL queries for different scenarios involving tables like SCHOOL, ADMIN, TRANSPORT, and COMPANY. Additionally, it covers the use of SQL commands for data retrieval and manipulation, along with specific queries and their expected outputs.

Uploaded by

Neeraj Tiwari
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/ 8

SQL NULL Value - A field with a NULL value is a field with no value.

Note: A NULL value is different from a zero value or a field that contains spaces. A field with a NULL
value is one that has been left blank during record creation!
Test for NULL Values- use the IS NULL and IS NOT NULL operators.
Syntax : IS NULL/IS NOT NULL
SELECT column_names
FROM table_name
WHERE column_name IS NULL/IS NOT NULL;

SQL UPDATE Statement - The UPDATE statement is used to modify the existing records in a table.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition

SQL DELETE Statement - The DELETE statement is used to delete existing records in a table.
Syntax: DELETE FROM table_name WHERE condition;

SQL Aggregate Functions - An aggregate function is a function that performs a calculation on a set
of values, and returns a single value. The most commonly used SQL aggregate functions are:
 MIN() - returns the smallest value within the selected column
 MAX() - returns the largest value within the selected column
 COUNT() - returns the number of rows in a set
 SUM() - returns the total sum of a numerical column
 AVG() - returns the average value of a numerical column
Note - Aggregate functions ignore null values (except for COUNT()).

SQL Aliases –
SQL aliases are used to give a table, or a column in a table, a temporary name.
Aliases are often used to make column names more readable.
An alias only exists for the duration of that query.
It is created with the AS keyword.
Example:
SELECT CustomerID AS ID
FROM Customers;

SQL GROUP BY Statement - In SQL, The Group By statement is used for organizing similar data into
groups. The data is further organized with the help of equivalent function. It means, if different rows in a
precise column have the same values, it will arrange those rows in a group.
 The SELECT statement is used with the GROUP BY clause in the SQL query.
 WHERE clause is placed before the GROUP BY clause in SQL.
 ORDER BY clause is placed after the GROUP BY clause in SQL.
Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)

MySQL HAVING Clause - The HAVING clause was added to SQL because the WHERE keyword cannot
be used with aggregate functions.
Example
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;

SQL Cartesian product - The CARTESIAN JOIN is also known as CROSS JOIN. In a CARTESIAN JOIN
there is a join for each row of one table to every row of another table. This usually happens when the
matching column or WHERE condition is not specified.
 In the absence of a WHERE condition the
CARTESIAN JOIN will behave like a CARTESIAN
PRODUCT . i.e., the number of rows in the result-
set is the product of the number of rows of the
two tables.
 In the presence of WHERE condition this JOIN will
function like a INNER JOIN.
 Generally speaking, Cross join is similar to an
inner join where the join-condition will always
evaluate to True
Syntax:
SELECT table1.column1 , table1.column2, table2.column1...
FROM table1
CROSS JOIN table2;

Difference between Natural join and Cross join in SQL


1. Natural Join :
Natural Join joins two tables based on same attribute name and data types. The
resulting table will contain all the attributes of both the tables but only one copy of
each common column.
Example:

Consider the given query


SELECT * FROM Student S NATURAL JOIN Marks M;
2. Cross Join :
Cross Join will produce cross or Cartesian product of two tables if there is no condition
specifies. The resulting table will contain all the attributes of both the tables including
duplicate or common columns also.
Example:

Consider the given query


SELECT * FROM Student S CROSS JOIN Marks M;

MySQL EquiJoin
The process is called joining when we combine two or more tables based on some common columns and
a join condition. An equijoin is an operation that combines multiple tables based on equality or
matching column values in the associated tables. We can use the equal sign (=) comparison
operator to refer to equality in the WHERE clause.
Syntax:
SELECT column_name (s)
FROM table_name1, table_name2, ...., table_nameN
WHERE table_name1.column_name = table_name2.column_name;
Question 1: Consider the following tables SCHOOL and ADMIN and answer this question : Give the
output the following SQL queries :
 Select Designation Count (*) From Admin Group By Designation Having Count (*) <2;
 SELECT max (EXPERIENCE) FROM SCHOOL;
 SELECT TEACHER FROM SCHOOL WHERE EXPERIENCE >12 ORDER BY TEACHER;
 SELECT COUNT (*), GENDER FROM ADMIN GROUP BY GENDER;

Question 2: Write SQL qureries for (i) to (iv) and find outputs for SQL queries (v) to (viii), which are
based on the tables TRANSPORT and TRIE

i) To display NO, NAME, TDATE from the table TRIP in descending order of NO.
ii) To display the NAME of the drivers from the table TRIP who are traveling by transport vehicle
with code 101 or 103.
iii) To display the NO and NAME of those drivers from the table TRIP who travelled between ‘2015-
02-10’ and ‘2015-04-01’.
iv) To display all the details from table TRIP in which the distance travelled is more than 100 KM in
ascending order of NOP
v) SELECT COUNT (*), TCODE From TRIP GROUP BY TCODE HAVNING COUnT (*) > 1;
vi) SELECT DISTINCT TCODE from TRIP;
vii) SELECT A.TCODE, NAME, TTYPE FROM TRIP A, TRANSPORT B WHERE A. TCODE = B. TCODE AND
KM < 90;
viii) SELECT NAME, KM *PERKM FROM TRIP A, TRANSPORT B WHERE A. TCODE = B. TCODE AND A.
TCODE = 105′;

Answer:

i) SELECT NO, NAME, TDATE FROM TRIP ORDER BY NO;


ii) SELECT NAME FROM TRIP WHERE TCODE = 101 OR TCODE = 103;
iii) SELECT NO AND NAME FROM TRIP WHERE ‘2015-02-10’ < TDATE < ‘2015-04-01’;
iv) SELECT NO, NAME, TDATE, KM, TCODE FROM TRIP WHERE KM >100 ORDER BY NOP;
v)
Count(*) Distinct(TCODE)
2 101
2 102
vi)
Distinct(TCODE)
101
103
102
104
105

vii)
A.TCODE NAME TTYPE
104 Aan Kumar CAR
105 Veena SUV
101 Rajpal Kirti VOLVO BUS

viii)
NAME KM*PERKM
Veena 1200

Question 3: Write SQL commands for the queries (i) to (iv) and output for (v) & (viii) based on a table
COMPANY and CUSTOMER.
1. To display those company name which are having prize less than 30000.
2. To display the name of the companies in reverse alphabetical order.
3. To increase the prize by 1000 for those customer whose name starts with „S?
4. To add one more column totalprice with decimal] 10,2) to the table customer
5. SELECT COUNT(*) , CITY FROM COMPANY GROUP BY CITY;
6. SELECT MIN(PRICE), MAX(PRICE) FROM CUSTOMER WHERE QTY>10;
7. SELECT AVG(QTY) FROM CUSTOMER WHERE NAME LIKE “%r%;
8. SELECT PRODUCTNAME,CITY, PRICE FROM COMPANY, CUSTOMER WHERE
COMPANY. CID=CUSTOMER.CID AND PRODUCTNAME=”MOBILE”;

Answer:

1. SELECT NAME FROM COMPANY WHERE COMPANY.CID=CUSTOMER. CID AND PRICE <
30000;
2. SELECT NAME FROM COMPANY ORDER BY NAME DESC;
3. UPDATE CUSTOMER SET PRICE = PRICE + 1000 WHERE NAME LIKE ‘S%’;
4. ALTER TABLE CUSTOMER ADD TOTALPRICE DECIMAL(10,2);

Question 4: Write SQL commands for the queries (i) to (iv) and output for (v) to (viii) based on the
tables Watches’ and Sale given below.
1. TO DISPLAY ALL THE DETAILS OF THOSE WATCHES WHOSE NAME ENDS WITH ‘TIME’
2. TO DISPLAY WATCH’S NAME AND PRICE OF THOSE WATCHES WHICH HAVE PRICE RANGE
IN BE-TWEEN 5000-15000.
3. TO DISPLAY TOTAL QUANTITY IN STORE OF UNISEX TYPE WATCHES.
4. TO DISPLAY WATCH NAME AND THEIR QUANTITY SOLD IN FIRST QUARTER;
5. SELECT MAX (PRICE), MIN(QTY_STORE) FROM WATCHES;
6. SELECT QUARTER, SUM(QTY SOLD) FROM SALE GROUP BY QUARTER;
7. SELECT WATCH_NAME, PRICE, TYPE FROM WATCHES W, SALE S WHERE W.
WAT£H1D!=S.WATCHID; (viii) SELECT WATCH_NAME, QTYSTORE, SUM (QTY_SOLD),
QTY_STORESUM (QTYSOLD) “STOCK” FROM WATCHES W, SALE S WHERE W. WATCHID =
S.WATCHID GROUP BY S.WATCHID;

Answer:
1. SELECT * FROM WATCHES WHERE WATCH_NAME LIKE ‘%TIME’
2. SELECT WATCH_NAME, PRICE WATCH WHERE PRICE BETWEEN 5000 AND 15000;
3. SELECT SUM (QTY STORE) FROM WATCHES WHERE TYPE LIKE ‘UNISEX’;
4. SELECT WATCHNAME, QTY SOLD FROM WATCHES W,SALE S WHERE W. WATCHID = S. WATCHID
AND QUARTER = 1;

Question 5: Answer the questions (a) and (b) on the basis of the following tables SHOP and
ACCESSORIES.
(a) Write the SQL queries:
i) To display Name and Price of all the Accessories in ascending order of their Price.
ii) To display Id and SName of all Shop located in Nehru Place.
iii) To display Minimum and Maximum Price of each Name of Accessories.
iv) To display Name, Price of all Accessories and their respective SName where they are available.

(b) Write the output of the following SQL


i) SELECT DISTINCT NAME FROM ACCESSORIES WHERE PRICE> =5000;
ii) SELECT AREA, COUNT(*) FROM SHOPPE GROUP BY AREA;
iii) SELECT COUNT (DISTINCT AREA) FROM SHOPPE;
iv) SELECT NAME, PRICE*0.05 DISCOUNT FROM ACCESSORIES WHERE SNO IN (‘S02‘,S03‘);
Answer:
(a)
i) SELECT Name, Price FROM ACCESSORIES ORDER BY Price Asc;
ii) SELECT ID SName FROM SHOP WHERE Area=”Nehru Place”;
iii) SELECT Name, max (Price); min(Price) FROM ACCESSORIES, Group By Name;
iv) SELECT Name,price, Sname FROM ACCESSORIES, SHOP WHERE SHOE
ID=ACCESSORIES.ID;

You might also like