Day 7: Advance SQL for Data Science | By Sunita Rawat |: January, 2023

Create View Salesman_info
As
select s.salesmanid,s.city, count(customerid) no_of_sales from salesman s
left join customer c on c.salesmanid = s.salesmanid
group by 1;
  1. Views can hide complexity.
  2. Views can be used as a security mechanism.
  3. Views can simplify the supporting legacy code.
  1. Scalar functions, ie return a single value.
  2. Table-valued functions, ie, return a collection of tables.
  3. Multi-table value functions ie return a set of tables.
DELIMITER $$
CREATE FUNCTION Calculate_Age
(
DOB date
)
RETURNS INT DETERMINISTIC
BEGIN
RETURN YEAR(CURRENT_DATE()) - YEAR(DOB);
END$$
DELIMITER ;
Inquiry :::: SELECT Calculate_Age(‘1988–02–29’) as DOB;

Execute a Stored Procedure

  1. A stored procedure can return zero, one, and multiple values.
  2. A stored procedure can also return a database of tables as required using multiple SQL queries.
  3. We can call a function from a stored procedure.
  4. A stored procedure can have input/output values ​​and parameters.
  5. We cannot use select/where/having statement with SPs.
  6. We can use insert/update/delete and select with stored procedure.
  7. An SQL stored procedure can execute dynamic SQL.
DELIMITER $$
CREATE PROCEDURE Proc_Saleman_Info_Data()
BEGIN
## Type 1
select s.salesmanid,s.city, count(customerid) no_of_sales from salesman s
left join customer c on c.salesmanid = s.salesmanid
group by 1;

## Type 2
insert into salesman
values(5008,'Shikha rawat','India',12);

END$$
DELIMITER ;

call Proc_Saleman_Info_Data();
  1. https://www.w3schools.com/sql/default.asp
  2. https://www.geeksforgeeks.org/

Source link