SQL Complete Course
SQL Complete Course
C : CREATE
R: READ
U: UPDATE
D: DELETE
TYPES OF DATABASE
RELATIONAL : (RDBMS) DATA STORED IN ONE OR MORE TABLES, TABLES HAVE ROWS AND COLUMNS
AND HAVE UNIQUE KEYS
(NON SQL) NON REALATIONAL: stored in traditional table, key value stores
WHY SQL?
1.PROCESSING SPEED
4.RECTRICTED ACCESS
*[LEARN ABOUT TYPES OF DATA TYPES AND NUMERIC DATATYPES AND DATE TYPES]*
CRUD OPERATIONS:
DDL: DML: DCL: TCL:
PRIMARY KEY
AUTOINCREMENT: Automatically fill the details by giving starting key and the identity command
FOREIGN KEY: THE KEY THAT IS IN ANOTHER TABLE WHICH IS THE REASIN WE ARE JOINING THE TABLE.
COMPOSITE KEY: GIVING MORE THAN 1 PRIMARY KEY IS CALLED COMPOSITE KEY.
ROLL_NO INTEGER,
NAME VARCHAR(20),
STREAM VARCHAR(20),
);
WITH ABOVE QUERY YOU CAN CREATE A TABLE AND INSERT VALUES
ALTER TABLE STUDENT_DETAILS ADD PHONE_N0 BIGINT----(DDL) THIS IS USED TO ADD MORE
COLUMNS
TRUNCATE TABLE STUDENT_DETAILS;----(DML) THIS WILL DELETE THE VALUES OF THE TABLE
BUT NOT THE TABLE.
SQL OPERATORS:
ARTHEMATIC OPERATIONS
+ ADD
- SUBRACT
* MULTIPLY
/ DIVIDE
% MODULO
BITWISE OPEARTIONS
COMPARISION OPERATORS
= EQUAL TO
> GREATER THAN
< LESS THAN
>= GREATER THAN OR EQUAL TO
<= LESS THAN OR EQUAL TO
<> NOT EQUAL TO
COMPOUND OPERATORS
+= ADD EQUALS
-= SUBTRACT EQUALS
*= MULTIPLY EQUALS
/= DIVIDE EQUALS
%= MODULO EQUALS
&= BITWISE AND EQUALS
^-= BITWISE EXCLUSIVE EQUALS
\*= BITWISE OR EQUALS
LOGICAL OPERATORS AGGREGATE AND QUERY
ALL OPERATORS
AND BETWEEN
ANY IS NULL: EMPTY
BETWEEN NOT NULL: NOT EMPTY
EXISTS DISTINCT : NO DUPLICATES
IN AVG : AVERAGE FUNCTION
LIKE COUNT: COUNTIN NO. OF ENTRYS
NOT MAX: MAXIMUM
OR MIN: MINIMUM
SOME SUM: AGGREAGATE VALUE
LIMIT
STRING FUNCTIONS:
WILDCARD: “LIKE”---
__: 2 CHARACTERS
WHERE Finds any values that have "r" in the second position
CustomerName LIKE
'_r%'
WHERE Finds any values that start with "a" and are at least 2
CustomerName LIKE characters in length
'a_%'
WHERE Finds any values that start with "a" and are at least 3
CustomerName LIKE characters in length
'a__%'
WHERE ContactName Finds any values that start with "a" and ends with "o"
LIKE 'a%o'
WHERE CustomerName LIKE Finds any values that starts with "a"
'a%'
WHERE CustomerName LIKE Finds any values that ends with "a"
'%a'
WHERE CustomerName LIKE Finds any values that have "or" in any position
'%or%'
WHERE CustomerName LIKE Finds any values that have "r" in the second position
'_r%'
WHERE CustomerName LIKE Finds any values that starts with "a" and are at least
'a__%' 3 characters in length
WHERE ContactName LIKE 'a Finds any values that starts with "a" and ends with
%o' "o"
SELECT TOP 3 MARKS FROM STUDENTS ------ FILTERING BASED ON THE NO. OF ENTRIES WE
WHERE MARKS>95
ORDER BY ASC /DESC ------- THIS WILL GIVE IN ASC/DESC ORDER.
SELECT NAME, LEN(NAME) AS LEN_NAME FROM STUDENT_DETAILS----- THIS TO FIND
THE LENGTH OF THE CHARACTERS.
AS IS ALIAS FOR GIVING NAME. LIKE NEW NAME.
SELECT CHARINDEX('A','SAILAJA')--- THIS IS CHARACTER INDEX WHIH SHOWS THE
INDEX COUNT OF ALPHABET IN GIVEN CHARACTER.
SELECT CONCAT('SAILAJA',' ','MAGGIDI') AS FULL_NAME ---- TO CONCATE
SELECT *FROM STUDENT_DETAILS ------ TO SELECT THE NAME THAT STARTS WITH
LETTER S
WHERE LEFT(NAME,1) IN('S')
SELECT *FROM STUDENT_DETAILS ------TO SELECT THE NAME THAT ENDS WITH
LETTER UWHERE LEFT(NAME,1) IN('U')
SELECT *FROM STUDENT_DETAILS
WHERE LEFT(NAME,1) IN('A','E','I','O','U') AND RIGHT(NAME,1)
IN ('A','E','I','O','U') --------TO START AND END WITH AEIOU. HERE ‘IN’
FUNCTIONS IS TO CHECK THE GIVEN CHARACTER IS PRESENT IN THE TABLE OR NOT.
CAST,YEAR,MONTH,DAY,TIME,DATEADD,DATEDIFF,DATEPART
SELECT
GETDATE()CURRENT_,DAY(GETDATE())DAY_,MONTH(GETDATE())MONTH_,YEAR(GETDATE())
YEAR_;
SELECT CAST( 11.9 AS VARCHAR(300)); -- USED TO CHANGE THE DATATYPE.
SELECT CAST(GETDATE() AS TIME); --- THIS GIVES TIME
SELECT DATEADD(YEAR,1, GETDATE()) ---- TO KNOW EXCATLY AFTER 1 YEAR.
MATHEMATICAL FUNCTIONS
SELECT POWER(5678,2); ----- FIRST NO. DENOTES BASE NUMBER AND SECOND
DENOTES THE POWER
SELECT ROUND(35678.3456,2)----- HERE IM TAKING 2 I.3 I WANT 2 VALUES AS
DECIMAL.
SELECT FLOOR(25.75) -----OUTPUT(25)--- CLOSEST INTEGER.IT HAS 2 CONDITIONS
i.e NO>25.75 OR NEAREST TO 25.75
SELECT CEILING(25.75)----OUTPUT(26)---- HERE NO<25.75 OR NEAREST INTEGER
AGGREGATE FUNCTION
GROUP BY: GROUPING THE SIMILAR DATA IN THE GIVEN TABLE and this works with aggregate
functions only.
HAVING: THIS FUNCTION WORKS AFTER GROUP BY AND WITH AGGREGATE VALUES ONLY. It
should be worked only with group clause. And with aggregate only.
SELECT SUM(Sales)TOTAL_SALES
from Orders
group by Region
having sum(sales)>1000;
JOINS IN SQL
Left join
Right join
Inner join
Cross join
Self join
Full outer join
ORDER OF EXECUTION
FROM AND JOINS
WHERE
GROUP BY
HAVING
SELECT
DISTINCT
ORDER BY
LIMIT/OFFSET
CASE: The CASE expression 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.
EG:
select
case
when Sales>10000 then 'gain'
when Sales between 10000 and 5000 then 'ok'
else 'loss'
end as 'status'
,*
from Orders
WE CAN add joins also below…
Eg: SELECT * , RANK() OVER (ORDER BY Sales desc) rnk from Orders
DENSE RANK : The DENSE_RANK() is a window function that assigns ranks to rows in
partitions with no gaps in the ranking values. If two or more rows in each partition have
the same values, they receive the same rank. The next row has the rank increased by
one.
ROW NUMBER: The Row_Number() function can provide a unique sequential number
for each row within the result set for a given SQL query.
EG: SELECT * , ROW NUMBER() OVER (ORDER BY Sales desc) ROW_NUM from
Orders
PARTITION BY : This divides the rows or query result set into small partitions
ROW BWTWEEN : It specifies a fixed number of rows that precede or follow the current row
regardless of their value.
PIVOT TABLE: PIVOT rotates a table-valued expression by turning the unique values from
one column in the expression into multiple columns in the output. And PIVOT runs
aggregations where they're required on any remaining column values that are wanted in
the final output.
EG: select * from
(select b.order_id, concat(a.cust_first_name,' ',a.cust_last_name)full_name from
customers a
inner join orders b on a.cust_id=b.cust_id)a
pivot(count(order_id) for full_name in (['sriraj Rangu' ],['sailaja maggidi'],
['james camaroon'])
)pivot_table;
OR
ELECT* from
(select order_id, cust_id from orders)a
pivot(
count(order_id) for cust_id in ([1],[4],[6])
)pivot_table;
CTE IS THE SUBSET OF THE DATA WE HAD AND WE CAN USE IN MANY TIMES TO SOLVE OUR
QUERIES. It is a temporary table extracting from main table