Professional Documents
Culture Documents
To select all columns from a table (Customers) for rows where the Last_Name column
has Smith
To query the same table, retrieving all columns for rows in which
the First_Name column's value begins with any letter and ends with "en", use this
statement:
+------------+------------+-----------+
+------------+------------+-----------+
+------------+------------+-----------+
If you used the % wild card instead (e.g., '%en') in the example above, the result set
might look like:
+------------+------------+-----------+
+------------+------------+-----------+
+------------+------------+-----------+
The following SQL statement selects all customers from the country "Germany" AND the
city "Berlin", in the "Customers" table:
Example
OR Operator Example
The following SQL statement selects all customers from the city "Berlin" OR "München", in the
"Customers" table:
Example
You can also combine AND and OR (use parenthesis to form complex expressions).
The following SQL statement selects all customers from the country "Germany" AND the city
must be equal to "Berlin" OR "München", in the "Customers" table:
Example
Using function
1.Find the average salary of instructor in computer science department with title avg_salary
Select count(*)
From course;
2. Display only those department where average salary of the employee is more than 60,000
Table Emp_Mstr(Branch_no, Emp_no) branch_no start with B and Emp_no strat with
E e.g. B1 as branch no and E1 as employee number
5. Find out the total number accouts segregated on the basis of account type per branch
Table ACCT_MSTR(branch-no, type , acct_no) A/C type is either CA OR SB
Reservation table
Id | CustomerId | Day |
+----+------------+------------+
| 1 | 1 | 2009-11-22 |
| 2 | 2 | 2009-11-28 |
| 3 | 2 | 2009-11-29 |
| 4 | 1 | 2009-11-29 |
| 5 | 3 | 2009-12-02 |
+----+------------+------------+
Scalar subqueries
4. Display the name of the customer from the Customers table, whose reservation has Id
equal to 5 in the Reservations table
mysql> SELECT Name FROM Customers WHERE
-> CustomerId=(SELECT CustomerId FROM Reservations WHERE Id=5);
Table subqueries
6. Display all cars that cost below the average price of all cars in the table.
Stored Procedure
To call procedure
mysql> call GetAllProduct();
mysql>DELIMITER //
mysql>CREATE PROCEDURE dispmeaasge()
BEGIN
SELECT ‘WELCOME TO MYSQL’;
END //
mysql>DELIMITER ;
To call procedure
mysql> call dispmeassaget();
Example of IN parameter
3. Create table offices(company_name, add1, add2, country) insert 5 records with different
country name such as USA, UK. Japan, India etc
Write procedure to display company name for specific country given by user
mysql>DELIMITER //
mysq>CREATE PROCEDURE GetOfficeByCountry(IN countryName VARCHAR(25))
BEGIN
SELECT *
FROM offices
WHERE country = countryName;
END //
mysql> DELIMITER ;
mysql>DELIMITER $$
mysql>CREATE PROCEDURE CountOrderByStatus(IN orderStatus VARCHAR(25),
OUT total INT)
BEGIN
SELECT count(orderNumber) INTO total
FROM orders
WHERE status = orderStatus;
END $$
mysql>DELIMITER ;
mysql>CALL CountOrderByStatus('Shipped',@total);
mysql>SELECT @total;
mysql>DELIMITER $$
mysql>CREATE
PROCEDURE `test2`(IN capital DOUBLE , IN rate INT , IN duration INT , OUT interest
DOUBLE)
BEGIN
SET interest = (capital * rate * duration)/100;
INSERT INTO `administrator1`(`interest`) VALUES(interest);
END $$
mysql>DELIMITER ;
mysql> call test2(100 , 7 , 3 , @primary_interest);
mysql> select @primary_interest;
+-------------------+
| @primary_interest |
+-------------------+
| 21 |
+-------------------+
1 row in set (0.00 sec)
mysql> select *
-> from administrator1;
+---------+------+----------+----------+
| capital | rate | duration | interest |
+---------+------+----------+----------+
| NULL | NULL | NULL | 21.00 |
+---------+------+----------+----------+
1 row in set (0.00 sec)
mysql> DELIMITER $$
mysql> CREATE PROCEDURE set_counter(INOUT count INT(4),IN inc INT(4))
BEGIN
SET count = count + inc;
END$$
mysql> DELIMITER ;
mysql> DELIMITER ;
sql> call usp_totalEmployeeByDeparment(2);
+-------+
| total |
+-------+
| 1 |
+-------+
mysql> call usp_GetEmployeeName(103, @name);
Query OK, 1 row affected (0.05 sec)
mysql> select @name;
+-------+
| @name |
+-------+
| Jack |
3. Stored procedure that accepts two parameters, adds them up and returns the result in OUT
parameter total.
mysql>DELIMITER $$
mysql>CREATE PROCEDURE mysum(IN par1 decimal(20,2),
IN par2 decimal(20,2),
OUT total decimal(20,2))
BEGIN
SET total = par1 + par2;
END $$
mysql>DELIMITER ;
mysql>Call mysum(10.5,25,@total);
mysql>select @total as t;
5. Accepts an order number and returns the total for that order: orderitems(order_num,
Item_name, item_price, Quantity)
CREATE PROCEDURE ordertotal(
IN onumber INT,
OUT ototal DECIMAL(8,2)
)
BEGIN
SELECT Sum(item_price*quantity)
FROM orderitems
WHERE order_num = onumber
INTO ototal;
END;
To invoke this new stored procedure you can use the following:
Mysql> CALL ordertotal(20005, @total);
SELECT @total;