SQL Full Course
SQL Full Course
SQL Full Course
for Beginners
(Telugu)
FULL COURSE CSE & IT Tutorial 4u
What’s in this video for
you? SQL Topics
1. Introduction to Database Theory
2. Introduction to SQL
3. Creating database & Tables in SQl
4. Basic Operations in SQL
5. SQL Built-in Functions
6. Aggregate & String Functions
7. Window Functions
8. Types of Joins
9. Triggers & Cursors
10. Procedures & Functions
11. Temporary Tables, SubQueries
12. Views CSE & IT Tutorial 4u
01 Introduction To Database
Theory
What is Database?
What is Data?
What is DBMS?
What is RDBMS?
Computer data is
information stored by a
computer. This information
may be in the form of text
documents, images, audio
clips, software programs, or
other types of data.
A database is an
organized collection
of structured
information (data),
typically stored
electronically in a
computer system.
CSE & IT Tutorial 4u
Popular
Database
Database Management
System (DBMS) is a
software for storing
and retrieving users’
data
SQL Dialects:
Creating Database:
Syntax:
CREATE DATABASE
database_name;
Example:
Syntax:
DROP DATABASE
database_name; Example:
Syntax:
Example:
Syntax:
DROP TABLE
table_name; Example:
Truncate Table:
Syntax:
TRUNCATE TABLE
table_name; Example:
Example:
Example:
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
INSERT INTO table_name VALUES (value1, value2, value3, ...);
1. Like
1. Select 2. In
2. Update 3. Between
3. Delete 4. Alias
4. Where 5. Wildcards
5. Null Values 6. Union
6. AND , OR, NOT 7. Exists
7. ORDER BY 8. Any,All
8. GROUP BY 9. Select into
9. Insert Into 10. Insert into select
10. HAVING
CSE & IT Tutorial 4u
DATA BASE
CustomerID CustomerName ContactName Address City PostalCode Country
SELECT EXAMPLE:
1. SELECT CustomerName, City FROM Customers;
2. SELECT * FROM Customers;
SELECT Syntax
SELECT EXAMPLE:
1. SELECT DISTINCT Country FROM Customers;
2. SELECT COUNT(DISTINCT Country)* FROM table_name;
UPDATE Syntax
UPDATE
EXAMPLE: SET ContactName='Juan' WHERE Country='Mexico';
UPDATE Customers
CSE & IT Tutorial 4u
DELETE
QUERY
The DELETE statement is used to delete existing records in a
table.
DELETE Syntax
DELETE FROM table_name WHERE condition;
DELETE EXAMPLE:
DELETE EXAMPLE:
IS NULL
SELECT column_names FROM table_name WHERE column_name IS NULL;
IS NOT NULL
SELECT CustomerName, ContactName, Address FROM Customers WHERE Address IS NOT NULL;
AND QUERY
AND Syntax
AND Example:
OR Syntax
SELECT column1, column2, … FROM table_name WHERE condition1 OR condition2 OR condition3
...;
OR Example:
NOT Example:
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.
ORDER BY Syntax
SELECT column1, column2, … FROM table_name ORDER BY column1, column2, ... ASC|DESC;
ORDER BY Example
SELECT * FROM Customers ORDER BY Country;
GROUP BY
QUERY
The GROUP BY statement groups rows that have the same values into summary rows, like
"find the number of customers in each country".
GROUP BY Syntax
ORDER BY Example
SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country;
HAVING QUERY
The HAVING clause was added to SQL because the WHERE keyword cannot be used with
aggregate functions.
HAVING Syntax
HAVING Example
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:
LIKE Syntax
Example
IN Syntax
IN Operator Examples
SELECT * FROM Customers WHERE Country IN ('Germany', 'France', 'UK');
BETWEEN QUERY
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.
BETWEEN Syntax
BETWEEN Example
SELECT * FROM Products WHERE Price BETWEEN 10 AND 20;
ALIAS
QUERY
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.
Example:
UNION Syntax
SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2;
The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL:
SELECT column_name(s) FROM table1 UNION ALL SELECT column_name(s) FROM table2;
Example
SELECT City FROM Customers UNION SELECT City FROM Suppliers ORDER BY City;
SELECT City FROM Customers UNION ALL SELECT City FROM Suppliers ORDER BY City;
ExistsQUERY
The EXISTS operator is used to test for the existence of any record in a subquery.
The EXISTS operator returns TRUE if the subquery returns one or more records.
EXISTS Syntax
Example
SELECT SupplierName FROM Suppliers WHERE EXISTS (SELECT ProductName FROM Products
ANY Syntax
SELECT column_name(s) FROM table_name WHERE column_name operator ANY (SELECT column_name
FROM table_name WHERE condition);
ANY Example
SELECT column_name(s) FROM table_name WHERE column_name operator ALL (SELECT column_name FROM table_name WHERE
condition);
ALL EXAMPLE:
● SELECT ProductName FROM Products WHERE ProductID = ALL (SELECT ProductID FROM OrderDetails WHERE Quantity = 10)
Numeric
Function
s
Built-in functions in SQL
● ABS
● POWER ● SQRT
● ROUND ● SQUARE
● SIN ● EXP
● COS ● LN
● TRUNC ● LOG
● TAN ● CEILING
● ASIN ● FLOOR
● ACOS ● SIGN
● ATAN
Syntax
ABS(number)
Example
Syntax
POWER(a, b)
Example
Return 4 raised to the second
power: SELECT POWER(4, 2);
ROUND
QUERY
The ROUND() function rounds a number to a specified number of decimal places.
Syntax
ROUND(number, decimals, operation)
Example
Round the number to 2 decimal places:
SELECT ROUND(235.415, 2) AS RoundValue;
SIN QUERY
The SIN() function returns the sine of a number.
Syntax
SIN(number)
Example
Syntax
COS(number)
Example
Return the cosine of a
number: SELECT COS(2);
TRUNCATE QUERY
TRUNCATE TABLE command deletes the data inside a table, but not the table itself.
Exampl
e
TRUNCATE TABLE Categories;
TAN QUERY
The TAN() function returns the tangent of a number.
Syntax
TAN(number)
Example
Return the tangent of a
number: SELECT TAN(1.75);
SQRT QUERY
Syntax
SQRT(number)
Example
Return the square root of a
number: SELECT SQRT(64);
SQUARE QUERY
The SQUARE() function returns the square of a number.
Syntax
SQUARE(number)
EXAMPLE
Return the square of a
number: SELECT
SQUARE(64);
EXP QUERY
The EXP() function returns e raised to the power of a specified number.
Syntax
EXP(number)
Example
Return e raised to the power of
1: SELECT EXP(1);
LN
QUERY
LN() function
SQL LN() function returns the natural logarithm of n, where n is greater than 0 and its base
is number equal to approximately 2.71828183.
Syntax:
LN(expression)
Exampl
SELECT
e LN(65) "natural_log of 65"
LOG QUERY
The LOG() function returns the natural logarithm of a specified number, or the logarithm of the
number to the specified base.
Syntax
LOG(number, base) -- Syntax for SQL Server
OR:
LOG(number) -- Syntax for Azure SQL Database, Azure SQL Data Warehouse, Parallel Data
Warehouse
Example
2: SELECT LOG(2);
CEILING QUERY
The CEILING() function returns the smallest integer value that is larger than or equal to a
number.
Syntax
CEILING(number)
Example
Return the smallest integer value that is greater than or equal to a
number: SELECT CEILING(25.75) AS CeilValue;
FLOOR
QUERY
The FLOOR() function returns the largest integer value that is smaller than or equal to a
number.
Syntax
FLOOR(number)
Example
Syntax
SIGN(number)
Example
Return the sign of a
number: SELECT
SQL
AGGREGATE
Function
s
Aggregation functions in SQL
● Min
● Max
● Averag
e
● Count
● Sum
MIN() Example
MAX() Example
AVG() Syntax
AVG() Example
COUNT() Syntax
COUNT() Example
SUM() Syntax
SUM() Example
● INITCAP
● LOWER ● LENGTH
● UPPER ● INSTR
● CONCAT
● LPAD
● RPAD
● LTRIM
● RTRIM
● REPLACE
● SUBSTR
Syntax
INITCAP(string)
Example
Consider following example return the capitalize string of given string
argument.
SQL> SELECT INITCAP('opal kole') "INITCAP" FROM DUAL;
INITCAP
Opal Kole
LOWER
QUERY
The LOWER() function converts a string to lower-case.
Syntax
LOWER(text)
Syntax
UPPER(text)
Example
Convert the text to upper-case:
SELECT UPPER('SQL Tutorial is FUN!');
CONCAT QUERY
Syntax
CONCAT(string1, string2, ...., string_n)
Example
Add two strings together:
Output : google.com
LTRIM
QUERY
The Note LTRIM() function removes leading spaces from a string.
Syntax
LTRIM(string)
Example
Remove leading spaces from a string:
SELECT LTRIM(" SQL Tutorial") AS LeftTrimmedString;
RTRIM
QUERY
The RTRIM() function removes trailing spaces from a string.
Syntax
RTRIM(string)
Example
Remove trailing spaces from a
string: SELECT RTRIM("SQL ") AS RightTrimmedString;
Tutorial
REPLACE QUERY
The REPLACE() function replaces all occurrences of a substring within a string, with a
new substring.
Syntax
REPLACE(string, from_string, new_string)
Example
Replace "SQL" with "HTML":
SELECT REPLACE("SQL Tutorial", "SQL", "HTML");
SUBSTR
QUERY
The SUBSTR() function extracts a substring from a string (starting at any position).
Syntax
SUBSTR(string, start, length)
OR
SUBSTR(string FROM start FOR length)
Example
Extract a substring from a string (start at position 5, extract 3
Syntax
LENGTH(string)
Example
Return the length of the string, in bytes:
SELECT LENGTH("SQL Tutorial") AS LengthOfString;
INSTR
QUERY
The INSTR() function returns the position of the first occurrence of a string in another
string.
Syntax
INSTR(string1, string2)
Example
Search for "3" in string "W3Schools.com", and return
position: SELECT INSTR(“google.com", "g") AS
Window
Function
s
Window functions in SQL
● OVER
● COUNT
● SUM
● ROW-NUMBER
● RANK
● DENSE-RANK
● LEAD
● LAG
The OVER clause is used to determine which rows from the query are applied to the function,
what order they are evaluated in by that function
Example:
Syntax
COUNT(expression)
Example
Return the number of products in the "Products" table:
SELECT COUNT(ProductID) AS NumberOfProducts FROM Products;
SUM QUERY
Syntax
SUM(expression)
Example
Return the sum of the "Quantity" field in the "OrderDetails" table:
SELECT SUM(Quantity) AS TotalItemsOrdered FROM OrderDetails;
ROW NUMBER QUERY
Example
The RANK() function is a window function that assigns a rank to each row within a partition of a
result set.
Syntax
RANK() OVER ( [PARTITION BY partition_expression, ... ]
ORDER BY sort_expression [ASC | DESC], ... )
Example
SELECT v,
RANK ()
OVER ( ORDER BY v )
rank_no FROM sales.rank_demo;
DENSE RANK
QUERY
The DENSE_RANK() is a window function that assigns a rank to each row within a partition of
a result set. Unlike the RANK() function, the DENSE_RANK() function returns consecutive rank
values. Rows in each partition receive the same ranks if they have the same values.
LEAD() is a window function that provides access to a row at a specified physical offset
which follows the current row. By using the LEAD() function, from the current row, you can
access data of the next row, or the row after the next row, and so on.
Syntax:
LEAD(return_value ,offset [,default])
Example:
SELECT month, net_sales, LEAD(net_sales,1) OVER ( ORDER BY month ) next_month_sale
LAG QUERY
LAG() is a window function that provides access to a row at a specified physical offset which
comes before the current row.
By using the LAG() function, from the current row, you can access data of the previous row, or the
row before the previous row, and so on.
Syntax:
LAG(return_value ,offset [,default])
OVER ( [PARTITION BY partition_expression, ... ]
ORDER BY sort_expression [ASC | DESC], ... )
Example:
SELECT month, brand_name, net_sales, LAG(net_sales,1) OVER ( PARTITION BY brand_name
ORDER BY month ) previous_sales FROM sales.vw_netsales_brands WHERE year = 2018
Types
of Joins
Joins
SQL JOINS are used to retrieve data from multiple tables. A SQL JOIN is
performed when two or more tables are combined in a SQL statement.
Syntax
The syntax for the INNER JOIN in SQL is:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column =
table2.column;
CSE & IT Tutorial 4u
Joins
Syntax:
The syntax for the LEFT OUTER JOIN in SQL is:
SELECT columns
FROM table1
LEFT [OUTER] JOIN table2
ON table1.column =
CSE & IT Tutorial 4u
table2.column;
Joins
Syntax
The syntax for the RIGHT OUTER JOIN in SQL is:
SELECT columns
FROM table1
RIGHT [OUTER] JOIN table2
ON table1.column =
CSE & IT Tutorial 4u
table2.column;
Joins
SELECT columns
FROM table1
FULL [OUTER] JOIN table2
ON table1.column = CSE & IT Tutorial 4u
Temporary
Tables,
SubQueries
Temporary
Tables
Temporary tables are stored in tempdb. They work like a regular table in that
you can perform the operations select, insert and delete as for a regular table.
They are specified with the prefix #, for example
#table_name There are 2 Types of Temporary Tables:
1 . Local Temporary Tables
2. Global Temporary Tables
# Syntax AS
(c1 INT);
create table
#table_name CREATE PROCEDURE sp_use_tempt
( AS
column_name BEGIN
) END
CSE & IT Tutorial 4u
Global Temporary Table
Type of Subqueries
● Single row subquery : Returns zero or one row.
● Multiple row subquery : Returns one or more rows.
● Multiple column subqueries : Returns one or more columns.
● Correlated subqueries : Reference one or more columns in the outer SQL
statement. The subquery is known as a correlated subquery because the subquery
is related to the outer SQL statement.
● Nested subqueries : Subqueries are placed within another subquery.
Views in SQL are kind of virtual tables. A view also has rows and columns as they are in
a real table in the database. We can create a view by selecting fields from one or more
tables present in the database. A View can either have all the rows of a table or specific
rows based on certain condition.
Syntax:
We have learned about creating a View, but what if a created View is not needed any
more? Obviously we will want to delete it. SQL allows us to delete an existing View.
We can delete or drop a View using the DROP statement.
Syntax:
DROP VIEW view_name;
Example
DROP VIEW [Brazil Customers];
There are certain conditions needed to be satisfied to update a view. If any one of
these conditions is not met, then we will not be allowed to update the view.
Syntax:
1. Implicit Cursor
2. Explicit Cursor
Microsoft SQL Server supports the following two fetch options for data:
3. FORWARD_ONLY - Specifies that the cursor can only be scrolled from the first to the last row.
4. SCROLL - It provides 6 options to fetch the data (FIRST, LAST, PRIOR, NEXT, RELATIVE, and
ABSOLUTE). CSE & IT Tutorial 4u
Procedure
s
Procedure
s
What is a Procedure?
Procedures are the subprograms which can be created and saved in the database as
database objects.
A procedure in SQL (often referred to as stored procedure), is a reusable unit that
encapsulates the specific business logic of the application. A SQL procedure is a group of
SQL statements and logic, compiled and stored together to perform a specific task.
1. Increased Cost
2. Complexity
3. Currency Maintenance
4. Database Failure
5. Huge Size
6. Difficult Backup And
Recovery
7. Confidentiality, Privacy, and
Security CSE & IT Tutorial 4u
DBMS Applications
Three Tier
Architecture
Database Schema
CSE & IT Tutorial 4u