SQL Notes
SQL Notes
SQL Notes
------Distinct-----------
- The SELECT DISTINCT statement is used to return only distinct (different) values.
Inside a table, a column often contains many duplicate values; and sometimes you only want
to list the different (distinct) values.
syntax - :
--------WHERE--------------------
- The WHERE clause can be combined with AND, OR, and NOT operators.
- The AND and OR operators are used to filter records based on more than one condition:
The AND operator displays a record if all the conditions separated by AND are TRUE.
The OR operator displays a record if any of the conditions separated by OR is TRUE.
The NOT operator displays a record if the condition(s) is NOT TRUE.
- AND Syntax :-
- OR Syntax:-
- NOT Syntax
Q) The following SQL statement selects all fields from "Customers" where country is "Germany"
AND city must be "Berlin" OR "München"
-----------Order By------------
- The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records
in descending order, use the DESC keyword.
-------------INSERT INTO-------------
2. If you are adding values for all the columns of the table, you do not need to specify the
column names in the SQL query. However, make sure the order of the values is in the same
order as the columns in the table. Here, the INSERT INTO syntax would be as follows:
- The following SQL statement inserts a new record in the "Customers" table:
Example
The following SQL statement will insert a new record, but only insert data in the
"CustomerName", "City", and "Country" columns (CustomerID will be updated automatically):
Example
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');
-------------------NULL-----------------------
- 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!
- With null we can find out whether any particular coloumn is having any null (empty) values or
not
- It is not possible to test for NULL values with comparison operators, such as =, <, or <>.
We will have to use the IS NULL and IS NOT NULL operators instead.
-----------------------UPDATE---------------------
Syntax :-
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
- Be careful when updating records. If you omit the WHERE clause, ALL records will be
updated!
-------------------DELETE--------------------------
Syntax:-
- Note: Be careful when deleting records in a table! Notice the WHERE clause in the DELETE
statement. The WHERE clause specifies which record(s) should be deleted. If you omit the
WHERE clause, all records in the table will be deleted!
- Here we are using where to specify the change... If we don't write where then all the records
will be changed .
-------------------TOP OR LIMIT---------------------------
- The SELECT TOP clause is used to specify the number of records to return.
The SELECT TOP clause is useful on large tables with thousands of records. Returning a large
number of records can impact performance.
- Note: Not all database systems support the SELECT TOP clause. MySQL supports the LIMIT
clause to select a limited number of records, while Oracle uses FETCH FIRST n ROWS ONLY
and ROWNUM.
FOR MYSQL
- The MIN() function returns the smallest value of the selected column.
- The MAX() function returns the largest value of the selected column.
- SELECT MAX(column_name)
FROM table_name
WHERE condition;
------------Count-----AVG--------SUM-----------
- The COUNT() function returns the number of rows that matches a specified criterion.
- SELECT COUNT(column_name)
FROM table_name
WHERE condition;
- SELECT AVG(column_name)
FROM table_name
WHERE condition;
- SELECT SUM(column_name)
FROM table_name
WHERE condition;
***WILDCARDS
- 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:
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 ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"
Q) The following SQL statement selects all customers with a ContactName that starts with "a"
and ends with "o":
Q) Select all records where the value of the City column does NOT start with the letter "a".
- The following SQL statement selects all customers with a City starting with "b", "s", or "p":
- The following SQL statement selects all customers with a City starting with "a", "b", or "c":
- The two following SQL statements select all customers with a City NOT starting with "b", "s", or
"p":
OR
Q) Select all records where the first letter of the City is an "a" or "c" or "s".
Q) Select all records where the first letter of the City starts with anything from an "a" to an "f".
Q) Select all records where the first letter of the City is NOT an "a" or a "c" or an "f".
----------------------------IN-------------------------------
- IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
or:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
- The following SQL statement selects all customers that are NOT located in "Germany",
"France" or "UK":
---------------BETWEEN----------------------------
- The BETWEEN operator selects values within a given range. The values can be numbers,
text, or dates.
- The BETWEEN operator is inclusive: begin and end values are included.
Q) The following SQL statement selects all products with a ProductName between Carnarvon
Tigers and Chef Anton's Cajun Seasoning:
OR
-------------------ALIASES------------------------------------
- SQL aliases are used to give a table, or a column in a table, a temporary name.
***NOTE****** IF the Alias name contains space then use double quotes or square brackets
The following SQL statement creates an alias named "Address" that combine four columns
(Address, PostalCode, City and Country):
EG:
SELECT CustomerName AS Customer, ContactName AS [Contact Person]
FROM Customers;
SELECT CustomerName, Address + ', ' + PostalCode + ' ' + City + ', ' + Country AS Address
FROM Customers; -----------> V.IMP
----------------------------JOINS--------------------------------------
- A JOIN clause is used to combine rows from two or more tables, based on a related column
between them.
- (INNER) JOIN: Returns records that have matching values in both tables
- LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from
the right table
- RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from
the left table
- FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table
------------------------------INNER JOIN-----------------------------------
Syntax:
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
--Inner Join on three tables
- The RIGHT JOIN keyword returns all records from the right table (Employees), even if there
are no matches in the left table (Orders).
- The Left JOIN keyword returns all records from the Join table (Employees), even if there are
no matches in the right table (Orders).
- The FULL OUTER JOIN keyword returns all matching records from both tables whether the
other table matches or not. So, if there are rows in "Customers" that do not have matches in
"Orders", or if there are rows in "Orders" that do not have matches in "Customers", those rows
will be listed as well.
-----------------SELF Join----------------------------
- A self join is a regular join, but the table is joined with itself.
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
- The UNION operator is used to combine the result-set of two or more SELECT statements.
- Every SELECT statement within UNION must have the same number of columns
- The columns must also have similar data types
- The columns in every SELECT statement must also be in the same order
- Syntax:
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
EX:
- The UNION operator selects only distinct values by default. To allow duplicate values, use
UNION ALL:
EX:
-----------------------------GROUP By---------------------------
- The GROUP BY statement groups rows that have the same values into summary rows, like
"find the number of customers in each country".
- The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(),
SUM(), AVG()) to group the result-set by one or more columns.
- Syntax :-
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Eg - The following SQL statement lists the number of customers in each country, sorted high to
low:
Eg - With Join ( The following SQL statement lists the number of orders sent by each shipper )
- The HAVING clause was added to SQL because the WHERE keyword cannot be used with
aggregate functions.
Syntax :-
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
eg - The following SQL statement lists the number of customers in each country. Only include
countries with more than 5 customers:
eg - with Join
- The following SQL statement lists the employees that have registered more than 10 orders:
- The following SQL statement lists if the employees "Davolio" or "Fuller" have registered more
than 25 orders:
-----------------------CASE-----------------------------------------
- The CASE statement goes through conditions and returns a value when the first condition is
met (like an if-then-else statement). So, once a condition is true, it will stop reading and return
the result. If no conditions are true, it returns the value in the ELSE clause.
CASE Syntax
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
EG :
EG: The following SQL will order the customers by City. However, if City is NULL, then order by
Country:
--------------------------------------------------------------------SQL NULL
Functions---------------------------------------------------
1. IFNULL() - Return the specified value IF the expression is NULL, otherwise return the
expression:
Syntax:
- IFNULL(expression, alt_value)
eg: SELECT COALESCE(NULL, NULL, NULL, 'W3Schools.com', NULL, 'Example.com'); -> the
ans is 'W3Schools.com'
3. ISNULL() - Return the specified value IF the expression is NULL, otherwise return the
expression:
eg - SELECT REPLACE('SQL Tutorial', 'T', 'M'); -> The output is 'SQL MuMorial'
eg - SELECT REPLACE('SQL Tutorial', 'SQL', 'HTML'); -> The output is 'HTML Tutorial'
eg - SELECT REPLACE('ABC ABC ABC', 'a', 'c'); -> The output is 'cBC cBC cBC'
https://www.youtube.com/watch?v=VxiF_MgePL8
- if the ans is 78.98 for avg(city) the by rounding off as Round(Avg(city)) it will be 78
*****NOTE*****
- if we write this then the value in blank will be either deptname or any value with
aggregate function
- Group by claus always works with having and not with where to know wht see video 58 of gate
smashers
- SQL Server FLOOR() Function - The FLOOR() function returns the largest integer value that is
smaller than or equal to a number. (Basically removes decimal values from number)
eg - SELECT FLOOR(25.75) AS FloorValue; answer is 25
- The LOCATE() function returns the position of the first occurrence of a substring in a string.
- If the substring is not found within the original string, this function returns 0.
---------------------------------------MID()--------------------------------------------------------------
------------------------------------------------SQUARE()-------------------------------SQRT()-----------------------
---------------------
--------------------------trim()----------------------ltrim()----------------------------rtrim()--------------------------
- Following MySQL query returns the current date and time:
SELECT NOW();
Regex
Syntax: Select col1, col2 from table where columname regexp ‘pattern’
Note: be careful while writing patterns, if we write regexp ‘xyz ’ then it will also search for space,
here spacing is provided just for clarity. Do not put space in real life
6. [0-9] -> 0 to 9
7. ^ -> starting
8. $ -> ending
10. ‘kite$ | poor’ -> ending with kite or has poor together anywhere
18. . ->
19. * ->
Quantifiers
[ ]{n} -> any character or number inside the square brackets that occurs n number of times
where n is any whole number here.
[ ]{y,z} -> any character or number inside the square brackets that occurs at least y times but
less than z times
1. Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION.
Your result cannot contain duplicates.
2. Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. Your
result cannot contain duplicates.
3. Query the list of CITY names from STATION which have vowels (i.e., a, e, i, o, and u) as
both their first and last characters. Your result cannot contain duplicates.
select distinct(city) from station where city regexp "^[aeiou]" and city regexp
"[aeiou]$";
4. Query the list of CITY names from STATION that do not start with vowels. Your result
cannot contain duplicates.
select distinct(city) from station where city regexp "^[^AEIOU]";
5. Query the list of CITY names from STATION that do not end with vowels. Your result
cannot contain duplicates.
select distinct(city) from station where city regexp "^[^aeiou]" and city regexp
"[^aieou]$";
DOUBT
Q-25. Write an SQL query to fetch duplicate records having matching data
in some fields of a table.
Ans.
Candidate Key -> Candidate key is a single key or a group of multiple keys that
uniquely identify rows in a table.
- Apart from the primary key rest are called Alternative Key
- There should always be one primary key in a table and can't be more
than one.
Foreign Key - It is an attribute or set of attributes that references to
primary key of same table or another table
- It’s not necessary that the name of foreign key attribute should have
the same name as the primary key
- The table with the foreign key is called the child table, and the table with
the primary key is called the referenced or parent table.
- Referential integrity (Imp Concept) -> It means that the foreign key
takes reference from primary key
Super Key -> We can define a super key as a set of those keys that identify
a row or a tuple uniquely. The word super denotes the superiority of a key.
Thus, a super key is the superset of a key known as a Candidate key
(discussed in the next section). It means a candidate key is obtained from a
super key only.
Note: Two or more attributes in a table can together identify a table uniquely, so the
combination of such attributes is nothing but a super key only.
ER Model
Basically scene ye hai ki hum architect hai jisse customer ne apne ghar ka
bule - print diya hai and ab usse mujhe construct karni hai
Ab obvious sa baat hai ki tumhe blueprint design kar ke dene ka bhi ek
pattern hoga aisa nahi ki customer ne bola mere ko garage terris pe
chahiye toh tum bana doge… yahi scene ER mein bhi hai har ek type of
attribute ke liye ek pattern set kiya gaya hai and you have to follow it!
Here the association table( here “has”) has 2 columns definitely( P.K of both
the other linking tables) and these act as foreign key for other tables. And
since there is no repetition (being 1 to 1), any column among these 2 can
be primary key.
- Tables can be reduced here ( e.g has table can be merged with either
of the other 2 table)
- We can take Primary Key in Has table
- Here in relationship table - primary key will be from MANY side(as it is not
repetitive) which is also the primary key for Many side table.
Many to Many-
1NF-
Rules
EF CODD says- Table should not contain a multivalued attribute
A relation is in first normal form if every attribute in that relation is a single
valued attribute.
Conversion to 1NF-
- You must define the data items.
- The next step is ensuring that there are no repeating groups of data.
- The final rule of the first normal form, create a primary key for each table.
CLOSURE METHOD
- Prime Attribute means the attribute required to form the Candidate Key
- Non Prime Attribute means the attribute that are not required to form the
Candidate Key
Q1) R (A B C D)
(Read as A closure)
A+ = A B C D
B+ = B C D
C+ = C D
D+ = D
Candidate Key can only be A as it is the one who is determining the rest of the
attributes
CK { A }
Q2) R (A B C D)
A+ = A B C D
B+ = B C D A
C+ = C D A B
D+ = A B C D
Prime Attribute = A, B, C, D
Q3) R ( A B C D E )
*NOTE* Here see the right side value there E is not present, hence in order to put on
right side we have to put in on left side for every attribute
AE+ = A B E C D A
BE+ = B E C D A
CE+ = C E
DE+ = D E C A B
- X determines Y
- Y is determined by X
a) Trivial
- X -> Y
b) Non - Trivial
1. Reflexivity
2. Augmentation
3. Transitive**
4. Union
If X -> Y and X -> Z, then X -> YZ
5. Decomposition
6. Pseudo Transitivity
7. Composition
2NF-
Rules-
1.Table should be in 1NF
EXTRA:
Eg:
R (A B C D E F )
Find CK - { EC }
Find Prime Attributes - {E, C }
Find Non Prime Attributes - { A, B, D, F }
by the above it is proven that the table has Partial Dependency, Hence the
table is not in 2NF form, we have to check for all the FD here
3NF
Rules
all Functional Dependencies
LHS of (FD) is CK or Super Key (SK)
OR
Q1) R ( A B C D )
FD { AB -> C, C -> D }
CK is { AB }
Prime Attribute { A, B }
Q2) R ( A B C D )
AB+ = A B C D
DB+ (Here we replaced A from D because we can see that D is not present
on RHS) = D B A C
CK { AB, DB }
Prime Attributes { A, B, D }
Rules
- Should be in 3 NF
FD
(
Roll_no -> Name
Roll_no -> Voter_id
Voter_id -> age
Voter_id -> Roll_no
)
Solve -
CK { Roll_no, Voter_id }
- Both third and BCNF ensures lossless decomposition (infact all the
normal forms)
- Questn
R {AB->CD, D->A}
CK - {AB, BD}
PA- {A,B,D}
Non PA- {C}
Joining the decomposed tables with some condition. Here extra tuples appear
which are not in R. This is lossy decomposition.
Quick Revision
Question For Minimal Cover
If any Dependency is in BCNF then it’s clear that it will be in 3rd, 2nd
and 1st Normal Form
To check Highest Normal Form of any table check first BCNF, then
3rd and so on…
1. Check X covers Y - Take left hand side of Y and write its closure from
X
2. Check Y covers X - Take left hand side of X and write its closure from
FDs of Y.
3. If both are true , then X and Y are equivalent
Q1)
Q2)
Checking X covers Y or not