SQL (4)
SQL (4)
SQL Commands :
There are 5 SQL commands -
1.Data definition language (DDL) : Used to define structure of
the table.
DDL : CREATE , ALTER , DROP , TRUNCATE ,RENAME etc.
5.Data Query Language (DQL) : used to query the data from the
database.
DQL : SELECT
SQL Data types :
A data type in SQL defines what kind of data a column, variable can
hold
Numeric:
Numeric data types are used to store numbers.
Binary:
Binary data types are used to store binary data, such as images
and files.
● BINARY Fixed-length binary data
● VARBINARY Variable-length binary data for files, images, etc.
SQL Constraints :
SQL Constraints are rules or conditions that you apply to columns
in a table to ensure the data's integrity, accuracy, and reliability.
● NOT NULL
Ensures that a column cannot have empty values.
EmployeeName , Email
● UNIQUE
Ensures all values in a column are different.
Email , ProjectName
● PRIMARY KEY
A combination of NOT NULL and UNIQUE. It identifies each
record in a table uniquely.
EmployeeID , ProjectID
● FOREIGN KEY
A column that links to the Primary Key of another table. This
ensures referential integrity.
● CHECK
Ensures values in a column meet a specific condition.
● DEFAULT
Assigns a default value to a column if no value is provided
during insertion.
WHERE clause : The WHERE clause allows the user to filter the
data from the table. The WHERE clause allows the user to extract
only those records that satisfy a specified condition
ORDER BY :
Order by is used to print the values from the table in
order(ascending or descending)
For getting the values which starts with a and ends with h
For getting the values which starts with a and at least are 5
character sin length
Q3. Calculate the top selling product quantity wise in year 2021
month wise
WITH RANK_TABLE AS (
SELECT
Year(Date) AS Year,
Month(Date) AS Month,
Product,
SUM(Quantity_Sold) AS TotalQuantitySold,
RANK() OVER (PARTITION BY Month(Date) ORDER BY
SUM(Quantity_Sold) DESC) AS Rank
FROM
dbo.mock_sales_data
WHERE
Year(Date) = 2021
GROUP BY
Year(Date), Month(Date), Product
)
SELECT
Year,
Month,
Product,
TotalQuantitySold
FROM RANK_TABLE
where Rank = 1
Q4. give the product , month and year in which product was sold
higest, qtysold ,total price
WITH grouped_data AS (
SELECT
YEAR(Date) AS yr,
MONTH(Date) AS mon,
Product,
SUM(Quantity_Sold) AS qty_sold,
SUM(Unit_Price) AS unit_price_sum
FROM
dbo.mock_sales_data
GROUP BY
YEAR(Date), MONTH(Date), Product
),
final_table AS (
SELECT
*,
RANK() OVER (PARTITION BY Product ORDER BY
unit_price_sum DESC) AS rankedrow
FROM
grouped_data
)
SELECT Product,yr,mon,qty_sold,unit_price_sum
FROM final_table
where rankedrow = 1
Q5. Give the list of the topsales man with total quantity sold
year-monthly wise
WITH grouped_sales as (
select year(Date)yr,Month(Date)mon ,Salesperson sp
,SUM(Quantity_Sold) qty_sold
from dbo.mock_sales_data
group by year(Date),Month(Date),Salesperson
),
pertition as(
select yr,mon,sp,qty_sold, Rank()over(partition by yr, mon order by
qty_sold desc)rnkcol
from grouped_sales )
WITH grouped_sales as (
select year(Date)yr ,Salesperson sp ,SUM(Quantity_Sold) qty_sold
from dbo.mock_sales_data
group by year(Date),Salesperson
),
pertition as(
select yr,sp,qty_sold, Rank()over(partition by yr order by qty_sold
desc)rnkcol
from grouped_sales )