SELECT stock_quantity,quantity_sold, product_name,(t1.stock_quantity - t2.
quantity_sold ) as
Remaining
                  FROM tbl_Products t1 ,tbl_Sales t2
                  WHERE t2.product_id = t1.product_id;
--SALE FOR DATE------------------------------------------------------------------------------------------
SELECT sale_date, quantity_sold,product_name,(t1.stock_quantity - t2.quantity_sold )as
Remaining
FROM tbl_Products t1 ,tbl_Sales t2
WHERE t2.product_id = t1.product_id AND sale_date ='2024-03-21'
SELECT sale_date, quantity_sold,product_name,(t1.stock_quantity - t2.quantity_sold )as
Remaining
FROM tbl_Products t1 ,tbl_Sales t2
WHERE t2.product_id = t1.product_id AND sale_date BETWEEN '2024-03-18' AND '2024-03-
21'
-----------------------------------------------------------------------------------------------------------------
join 2 tables for no of sold
SELECT t1.quantity_sold, t2.product_name
FROM tbl_Sales t1
JOIN tbl_Products t2 ON t1.product_id = t2.product_id
WHERE t1.quantity_sold = 0;
--------------------------------------------------------------------------------------------------------------
--------------function----------------------------------------------------------------------------------------
create FUNCTION CalculateTotalSalePrice
    @UnitPrice DECIMAL(10, 2),
    @Quantity INT
)
RETURNS DECIMAL(10, 2)
AS BEGIN
     DECLARE @TotalPrice DECIMAL(10, 2);
     SET @TotalPrice = @UnitPrice * @Quantity;
     RETURN @TotalPrice;
END;
DECLARE @UnitPrice DECIMAL(10, 2) = 10.00;
DECLARE @Quantity INT = 5;
SELECT dbo.CalculateTotalSalePrice(55,5) AS TotalPrice
---------------------------------------------------------------------------------------------------------------
---------trigger----------------------------------
create trigger choco
on tbl_Products
for
insert,update ,delete
as
print 'you can not insert,update and delete this table i'
rollback;
------------------index-------------------------------------------------
CREATE INDEX IX_ProductName
ON tbl_Products (product_name);
ALTER INDEX IX_ProductName ON tbl_Products REBUILD;
ALTER INDEX IX_ProductName ON tbl_Products REORGANIZE;
----------------------------------------------------------------------------------------------------------------
------store procedure----------------
Alter Procedure USP_LoginCheck
@UserName varchar(50)
As
Begin
select * from tbl_Products where product_id=@UserName
End
Execute USP_LoginCheck '11'
Alter Procedure USP_Insert_Products
@product_id Int,
@product_name varchar(500),
@stock_quantity Int
As
Begin
        if (@product_id >= 5000)
        Begin
                Insert into tbl_Products values(@product_id,@product_name,@stock_quantity)
        End
        Else
        Begin
                   Select 'Invalid Prod ID' As Output
         End
End
Exec USP_Insert_Products 5005,'Rin Liq',150
--------------------------trigger--------------------
create trigger tbl_products1 on tbl_products
after insert
as
begin
declare @product_id int
declare @product_name varchar
declare @stock_quantity int
select @product_id = productlist.product_id from inserted productlist;
select @product_name =productlist.product_name from inserted productlist;
select @stock_quantity = productlist.stock_quantity from inserted productlist;
insert into tbl_products1 (product_id,product_name,stock_quantity)
values (@product_id,@product_name,@stock_quantity)
end
go
Tables: Insert, Delete, update, truncate, drop, select
                                    TABLES
SELECT
SELECT * FROM Table_Name;
   Count Distinct
SELECT COUNT(DISTINCT Column_Name) FROM Table_Name;
   WHERE Clause- used to filter records.
Syntax: SELECT Column1, Column2,…
      From table_name
      Where condition;
Operators
ORDER BY: sort the result set
Syntax: SELECT Column1, Column2, …
       From table_name
       Order by Column1, Column2, … asc|desc ;
AND Operator
       SELECT column1, column2, ...
       FROM table_name
       WHERE condition1 AND condition2 AND condition3 ...;
AND vs OR
The AND operator displays a record if all the conditions are TRUE.
The OR operator displays a record if any of the conditions are TRUE.
OR Operator
      SELECT column1, column2, ...
      FROM table_name
      WHERE condition1 OR condition2 OR condition3 ...;
NOT Operator
    SELECT column1, column2, ...
      FROM table_name
      WHERE NOT condition;
NOT LIKE
SELECT * FROM Customers
WHERE CustomerName NOT LIKE 'A%';
NOT BETWEEN
SELECT * FROM Customers
WHERE CustomerID NOT BETWEEN 10 AND 60;
NOT IN
SELECT * FROM Customers
WHERE City NOT IN ('Paris', 'London');
NOT Greater Than
SELECT * FROM Customers
WHERE NOT CustomerID > 50;
INSERT INTO
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Aggregate Functions = calculation on a set of values, and returns a single value.
often used with the GROUP BY clause
      MIN() - returns the smallest value within the selected column
      MAX() - returns the largest value within the selected column
      COUNT() - returns the number of rows in a set
      SUM() - returns the total sum of a numerical column
      AVG() - returns the average value of a numerical column
Aggregate functions ignore null values (except for COUNT()).
Views
TRIGGER