SQL material-3
SQL material-3
The UNION operator is used to combine the results of two or more SELECT statements.
Every SELECT statement within UNION must have the same number of columns, similar
data types
Union operator result will not include duplicate data.
Syntax:
SELECT column_name(s) FROM table1
UNION/UNION ALL
SELECT column_name(s) FROM table2;
Example:
select mid,mname from mobiles
union
select aid,aname from accessories;
Stored Procedure
A stored procedure is a prepared SQL code that can be saved, so the code can be reused
over and over again.
Syntax to create stored procedure:
DELIMITER //
CREATE procedure procedure name()
BEGIN
SQL query…
END
//
Syntax to execute a stored procedure:
Call procedurename;
Example:
DELIMITER //
CREATE procedure PROC111()
BEGIN
SELECT * from mobiles;
END
//
call PROC111;
INSERT INTO SELECT:
Copy all columns from one table to another table:
Syntax:
INSERT INTO table2 SELECT * FROM table1 WHERE condition;
Example:
INSERT INTO mobiles SELECT * FROM accessories;