Module 2 SQL
Module 2 SQL
Module 2 SQL
USE TRAINING;
--INSERT INTO PRODUCT VALUES(44,'PRODUCT1',25);
SELECT * FROM PRODUCT;
select count(*) as ct
from product;
select p_name,
sum(p_price) from product
--where p_desc <> 'Microsoft'
group by (p_name);
select p_name,
sum(p_price) as sum_price
from product
group by p_name;
select p_name,
max(p_price) as max_price
from product
group by p_name;
---
--ALTER TABLE EXERCISE
--ALTER TABLE PRODUCT ADD MFG_DATE DATE DEFAULT '2001-01-01' NOT NULL;
------------------------------AGGREGARTION
FUNCTIONS---------------------------------
select COUNT(p_price) as ct
,SUM(p_price) as sum_price
From PRODUCT;
select avg
(p_price )
from PRODUCT;
--CHECKSUM
SELECT P_PRICE,
CHECKSUM(CAST(P_PRICE AS VARCHAR(10)))
as C_PRICE
FROM PRODUCT
;
--https://stackoverflow.com/questions/7753302/what-is-the-difference-between-
checksum-agg-and-checksum#:~:text=CHECKSUM%20calculates%20a%20hash%20for,aggregated
%20checksum%20for%20each%20group.
--------------Mathematical FUNCTION
--ABS Function
SELECT ABS(-100);
--Log function
--SQUARE FUNCTION
SELECT p_PRICE,
SQUARE(p_PRICE) from PRODUCT
--CEILING FUNCTION
SELECT CEILING(-4.5);
--FLOOR FUNCTION
SELECT FLOOR(-1.5);
--ROUND FUNCTION
SELECT ROUND(235,2)
--POWER FUNCTION
SELECT POWER(3,3)
------------------------------DATE FUNCTIONS---------------------------------
--------------Current date and time
SELECT CURRENT_TIMESTAMP;
SELECT GETUTCDATE();
SELECT GETDATE();
SELECT SYSDATETIME();
SELECT SYSDATETIMEOFFSET()
SELECT YEAR(
'2017-08-25'
) AS DatePartString; --return as YEAR OF DATE
SELECT SWITCHOFFSET(
GETDATE(),'-10:40'
) AS NEW_DATE
--Construct date and time from parts
SELECT DATEFROMPARTS(2023,05,27) as new_date
SELECT DATETIMEOFFSETFROMPARTS ( 2010, 12, 31, 14, 23, 23, 1, 12, 0, 7 ) AS Result;
UPDATE TMP_PRODUCT
SET P_PRICE=0
--LOWER Function
SELECT LOWER('NEW YORK');
--UPPER Function
SELECT UPPER('new york');
--REVERSE Function
SELECT REVERSE('NEW YORK');
--SUBSTRING Function
SELECT SUBSTRING('SHUBHAM',2,4);
--REPLACE FUNCTION
SELECT replace ('NEW NEW YORK','NEW','OLD');
SELECT replace ('KEEP','EE','ET');
SELECT C_NAME,NEW_EMAIL,
CONCAT(C_NAME,'|',NEW_EMAIL)
AS NAME_EMAIL FROM CUSTOMER;
--CONCAT_WS
SELECT CONCAT_WS(',','NEW', ' YORK');
--ASCII
SELECT ASCII('z');
--LENGTH
SELECT LEN('NEW YORK');
SELECT LEN('AKSHAY');
SELECT *,
LEN(EMAIL) AS LEN_EMAIL
FROM CUSTOMER;
-------------------SYSTEM Functions
SELECT *,
CAST(P_PRICE AS NUMERIC(7,3)) as NUM_PRICE
FROM PRODUCT;
SELECT ISNUMERIC('test');
SELECT ISNUMERIC(12345);
----------------WINDOW FUNCTIONS
SELECT * FROM (
SELECT
*,
--RANK() OVER(PARTITION BY P_NAME ORDER BY P_PRICE DESC ) AS RNK
--,DENSE_RANK() OVER(PARTITION BY P_NAME ORDER BY P_PRICE DESC ) AS DENSE_RNK,
--ROW_NUMBER() OVER(ORDER BY P_ID) as ROW_NUM,
CUME_DIST() OVER( ORDER BY P_PRICE ) AS CUM_DIST
FROM
PRODUCT) a
WHERE DENSE_RNK=3 and p_name='Laptop';
select *,
RANK() OVER( ORDER BY P_PRICE, p_desc,P_expiry_date ) AS RNK_ASC,
--RANK() OVER( ORDER BY P_PRICE DESC ) AS RNK_DESC,
DENSE_RANK() OVER( ORDER BY P_PRICE ) AS DENSE_RNK,
ROW_NUMBER() OVER( ORDER BY P_PRICE ) AS RN_NUMBER,
CUME_DIST() OVER( ORDER BY P_PRICE ) AS CUM_DIST
FROM
PRODUCT;
SELECT * ,
DENSE_RANK() OVER (PARTITION BY p_name ORDER BY p_PRICE DESC) AS RN
FROM PRODUCT;
--FIRST_VALUE FUNCTION
select *,
FIRST_VALUE(p_desc) OVER( PARTITION BY P_NAME ORDER BY P_PRICE DESC ) AS RN FROM
PRODUCT
;
select *,
LAST_VALUE(p_price) OVER( PARTITION BY P_NAME ORDER BY P_PRICE DESC
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS RN_PRICE,
LAST_VALUE(p_desc) OVER( PARTITION BY P_NAME ORDER BY P_PRICE DESC
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS RN_desc FROM
PRODUCT
;
select *,
LAST_VALUE(p_price)
OVER( PARTITION BY P_NAME ORDER BY P_PRICE DESC
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS RN
FROM
PRODUCT
;
--LAST_VALUE FUNCTION
select *, LAST_VALUE(p_price) OVER( PARTITION BY p_name ORDER BY P_PRICE DESC
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS DESC_PRICE,
LAST_VALUE(p_desc) OVER( PARTITION BY p_name ORDER BY P_PRICE DESC
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS DESC_LAST_VALUE,
FIRST_VALUE(p_price) OVER( PARTITION BY P_NAME ORDER BY P_PRICE DESC
) AS FV_PRICE,
select *,
LEAD(P_PRICE,1) OVER( PARTITION BY P_NAME ORDER BY P_PRICE DESC ) AS RN1,
LEAD(P_PRICE,2) OVER( PARTITION BY
P_NAME
ORDER BY P_PRICE DESC ) AS RN2
FROM PRODUCT;
select *,
LAG(P_ID,1) OVER( ORDER BY p_id ) AS RN
FROM PRODUCT;
----NTILE
select *,
NTILE(2) OVER(
PARTITION BY P_NAME ORDER BY P_PRICE DESC) AS RN FROM PRODUCT;
select *, PERCENT_RANK()
OVER( PARTITION BY P_NAME ORDER BY P_PRICE ) AS RN,
CUME_DIST() OVER( PARTITION BY P_NAME ORDER BY P_PRICE ) AS CUM_DIST
FROM PRODUCT;
------CASE FUNCTION
SELECT *,
CASE
WHEN P_PRICE <500 THEN 'LOW'
WHEN P_PRICE BETWEEN 500 AND 1000 THEN 'MEDIUM'
ELSE 'HIGH'
END
AS P_PRICE_CATEGORY
FROM PRODUCT ;
--dropping a function
DROP function sales_discount;
USE TRAINING;