SQL Notes
SQL Notes
~~~~~~~~~~~~~~~~~
-----------------------------------------------------------------------------------
---
Data
~~~~~~~~~~~
Collection of information
Database
~~~~~~~~~~~~~
Collection of related information stored at one place.
Table
~~~~~~
Table consists of rows and columns
Rows
~~~~~~~~
Horizontal ones
Columns
~~~~~~~~~~
Vertical ones
1 Row = 1 Record
Each data is stored in 1 Record
DBMS
~~~~~~~~~~~
Database Management System
Examples of DBMS
~~~~~~~~~~~~~~~~~~
1. MySQL Open Source Oracle (Sun Microsystems)
2. SQL Server Microsoft
3. Oracle Oracle
4. PostgresSQL Open Source
CRUD Operations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C Create CREATE
R Read SELECT
U Update UPDATE
D Delete DELETE / DROP
Features of DBMS
~~~~~~~~~~~~~~~~~
-- Interface between Database and User
-- Software to store , retrieve, define and manage data in the database.
-- Easy CRUD Operations.
-- Takes care of authentication(security), authorization, concurrency, logging,
backup, optimization etc.
-- should support connection using other applications and other programming
languages.
RDBMS
~~~~~~~~~~~~~
Relational Database Management Systems
Examples of RDBMS
~~~~~~~~~~~~~~~~~~
1. MySQL Open Source Oracle (Sun Microsystems)
2. SQL Server Microsoft
3. Oracle Oracle
4. PostgresSQL Open Source
Datatypes in MySQL
~~~~~~~~~~~~~~~~~~~~~
20 number
"twenty" string
'twenty' string
String
~~~~~~~~~
set of characters
125 ASCII Characters
either double quotes ""
or single quotes ''
Examples:
~~~~~~~~~~
"abcd!@#$"
'sql'
"mysql"
"20"
Example
Text
~~~~~
-----------------------------------------------------------------------------------
--------------------------------------------------------------
Text Type Maximum number of bytes
-----------------------------------------------------------------------------------
----------------------------------------------------------------
TINYTEXT 255
TEXT 65,535
MEDIUMTEXT 16,777,215
LONGTEXT 4,294,967,295
-----------------------------------------------------------------------------------
-----------------------------------------------------------------
BLOB Type
~~~~~~~~~~~~~
TINYBLOB
BLOB
MEDIUMBLOB
LONGBLOB
-----------------------------------------------------------------------------------
-------------------------------------------
Numbers / Numericals
~~~~~~~~~~~~~~~~~~~~~~~
1. Whole Number 1, -1
2. Decimal Number 1.0, -1.0
Whole Number
~~~~~~~~~~~~~~~~
-----------------------------------------------------------------------------------
-------------------------------------------------------------
Whole Number Type Signed Range
-----------------------------------------------------------------------------------
----------------------------------------------------------------
TINYINT -128 to 127
SMALLINT -32,768 to 32,767
MEDIUMINT -8,388,608 to 8,388,607
INT -2,147,483,648 to 2,147,483,647
BIGINT -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
-----------------------------------------------------------------------------------
----------------------------------------------------------------
specifying size of digits for INT datatype is optional INT(10) OR INT
Decimal Number
~~~~~~~~~~~~~~~~~
DECIMAL(TOTAL_DIGITS, TOTAL_DECIMAL_DIGIT)
Example
~~~~~~~~~~~~~~~~~
123.4 => DECIMAL(4, 1)
123.45 => DECIMAL(5, 2)
12345.6789 => DECIMAL(9, 4)
123456789.0 => DECIMAL(9, 0)
12345.10 => DECIMAL(7, 2)
Date
~~~~~~~~~
-----------------------------------------------------------------------------------
----------------------------------------------------
Date Datatype Default Format Allowable Values
-----------------------------------------------------------------------------------
----------------------------------------------------
DATE YYYY-MM-DD 1000-01-01 to 9999-12-31
DATETIME YYYY-MM-DD HH:MI:SS 1000-01-01 00:00:00 to 9999-12-31 23:59:59
TIMESTAMP YYYY-MM-DD HH:MI:SS 1970-01-01 00:00:00 to 2037-12-31 23:59:59
YEAR YYYY 1901 to 2155
TIME HHH:MI:SS -838:59:59 to 838:59:59
-----------------------------------------------------------------------------------
---------------------------------------------------
Example
~~~~~~~~~~~~~
DATETIME Custom Date and Time / User Specified Date and Time
e.g. Date of Birth, Train Time, Bus Time
-----------------------------------------------------------------------------------
-------------------------------
-- comments in mysql
-- two hyphen symbols then space then comments
SHOW DATABASES;
SHOW TABLES;
-- to delete table
DROP TABLE student;
SHOW TABLES;
-- it will not throw error if the table is not available. it will show only warning
DROP TABLE IF EXISTS student;
-- column having NOT NULL and UNIQUE will automatically become PRIMARY KEY
CREATE TABLE student (
name VARCHAR(50) NOT NULL,
city VARCHAR(50) DEFAULT "bangalore",
aadhaar BIGINT NOT NULL UNIQUE
);
DESCRIBE student;
DROP TABLE student;
-----------------------------------------------------------------------------------
-------------------------------------------------
-- TASK 1
-- create customer table
+---------------+-----------------+----------+----------+-------------+---------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-----------------+----------+----------+-------------+---------+
| id | int | NO | PRI | NULL |
|
| name | varchar(50) | NO | | NULL | |
| mobile | int | YES | UNI | NULL |
|
| gender | char(1) | YES | | M |
|
+--------------+------------------+---------+----------+--------------+--------+
-----------------------------------------------------------------------------------
-------------------------------------------
-- TASK 2
-- create person table
+-------------+------------------+-------+-------+---------------+---------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+-------+-------+---------------+---------+
| id | int | NO | PRI | NULL | |
| name | varchar(50) | NO | | NULL | |
| aadhaar | bigint | YES | UNI | NULL | |
| city | varchar(50) | YES | | mysore | |
| state | varchar(50) | YES | | karnataka | |
| country | varchar(50) | YES | | india | |
+------------+------------------+--------+-------+----------------+-------+
-----------------------------------------------------------------------------------
------------------------------------------------------
-- ALTER -- RENAME
SHOW TABLES;
-- delete column
ALTER TABLE student
DROP COLUMN student_state;
DESCRIBE student;
-----------------------------------------------------------------------------------
-----------------------------------------------
-- TRUNCATE
~~~~~~~~~~~~~~~~
-- Only the data or all the records will be deleted and Table structure will not be
deleted
-----------------------------------------------------------------------------------
-----------------------------------------------------------
-- DML - Data Manipulation Language
-- INSERT
-- UPDATE
-- DELETE
-----------------------------------------------------------------------------------
-----------------------------------------------------
-- INSERT
~~~~~~~~~~~~~
SELECT *
FROM student;
SELECT *
FROM student;
SELECT *
FROM student;
SELECT *
FROM student;
-- it will throw error as check constraint is violated
INSERT INTO student
(name, city, age)
VALUES
("katappa", "dholakpur", 17);
-- ORDER BY
-- Default is ascending
-- ASC ascending
-- DESC descending
-- Ascending Order
SELECT *
FROM student
ORDER BY name;
-- Descending Order
SELECT *
FROM student
ORDER BY name DESC;
-- Constraints in MYSQL
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- NOT NULL
-- AUTO_INCREMENT
-- UNIQUE
-- DEFAULT
-- CHECK
-- PRIMARY KEY
-- FOREIGN KEY
----------------------------------------------------------------
-- clauses
-- WHERE clause
-- filter the rows
Mathematical Operators
~~~~~~~~~~~~~~~~~~~~~~~~~
< Less Than
> Greater Than
<= Less Than Or Equal To
>= Greater Than Or Equal To
= Equal To
<> Not Equal To
!= Not Equal To
Logical Operators
~~~~~~~~~~~~~~~~~~~~~
AND Logical And Operator
OR Logical Or Operator
NOT Logical Not Operator
-- equal to operator
SELECT *
FROM student
WHERE salary = 50000;
SELECT *
FROM student
WHERE city = "shimoga";
-- IN
-- NOT IN
-- search for students who are not from bangalore and mysore
SELECT *
FROM student
WHERE city NOT IN ("bangalore", "mysore")
ORDER BY city, name;
-- NOT
SELECT NOT 0;
SELECT NOT 1;
TASK
-- show all the students who are not from bangalore using NOT keyword and without
using IN keyword
SELECT *
FROM student
WHERE NOT city = "bangalore";
-- empty set
SELECT *
FROM student
WHERE city = "bangalore" AND city = "mysore";
-- empty set
SELECT *
FROM student
WHERE name = "naveed" AND city = "bangalore";
-- Logigal Operator OR
-- left condition OR right condition
-- true OR true => output
-- true OR false => output
-- false OR true => output
-- false OR false => empty set
-- empty set
SELECT *
FROM student
WHERE name = "sql" OR name = "java";
-----------------------------------------------------------------------------------
------------------------
-- BETWEEN
-- between certain range of numbers
-- BETWEEN -- AND
-- BETWEEN for numbers - should compare between from lowest number to highest
number
-- BETWEEN for characters - should compare from A to Z (alphabetically)
-- empty set
SELECT *
FROM student
WHERE salary BETWEEN 80000 AND 40000
ORDER BY salary DESC;
SELECT *
FROM student
ORDER BY city;
-- empty set
SELECT *
FROM student
WHERE city BETWEEN "u" AND "a"
ORDER BY city;
SELECT *
FROM student
WHERE city BETWEEN "gulbarga" AND "mysore"
ORDER BY city;
SELECT *
FROM student
WHERE city BETWEEN "mysore" AND "gulbarga"
ORDER BY city;
-----------------------------------------------------------------------------------
---------------------------------------------
-- LIKE
-- Wild Card Search
-- 1. % percentage symbol
-- 2. _ underscore symbol
-- % percentage symbol
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- zero or more unknown characters
-- multiple unknown characters
-- length of characters is also not known
-- _ underscore symbol
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- one single unknown character
USAGE of LIKE
~~~~~~~~~~~~~~
-- search for city whose starting character is "b" and ending character is "e"
SELECT *
FROM student
WHERE city LIKE "b%e";
-- search for name whose second character from start and sixth character from end
is "h"
SELECT *
FROM student
WHERE name LIKE "_h_____";
-- search for name whose third character from the last is "u"
SELECT *
FROM student
WHERE name LIKE "%u__";
-----------------------------------------------------------------------------------
---------------------------------------------------------
-- UPDATE
~~~~~~~~~~~~~~~~~~~
SELECT *
FROM student
WHERE name = "sachin";
-- DELETE
~~~~~~~~~~~~~
-- empty set
SELECT *
FROM student
WHERE name = "sachin";
-- caution
-- do not forget to use WHERE clause in case of UPDATE and DELETE operations
-----------------------------------------------------------------------------------
-------------------------------------------------------------
-- table branch
-- table employee
-- create table branch
CREATE TABLE branch (
branch_id INT PRIMARY KEY AUTO_INCREMENT,
branch_name VARCHAR(50) UNIQUE NOT NULL
);
DESCRIBE branch;
SELECT *
FROM branch
ORDER BY branch_name;
SELECT *
FROM branch
ORDER BY branch_id;
DESCRIBE employee;
DROP TABLE employee;
DESCRIBE employee;
DROP TABLE employee;
DESCRIBE employee;
DROP TABLE employee;
DESCRIBE employee;
DESCRIBE employee;
DESCRIBE employee;
DESCRIBE employee;
-- add foreign key without CONSTRAINT or without name
ALTER TABLE employee
ADD FOREIGN KEY (br_id) REFERENCES branch (branch_id);
DESCRIBE employee;
-- pattern the mysql server follows to give name for FOREIGN KEY
-- tableName_ibfk_serial
DESCRIBE employee;
SELECT *
FROM employee
ORDER BY job_desc, emp_name;
-----------------------------------------------------------------------------------
-------------------------------------------------
ONLINE RESOURCES
~~~~~~~~~~~~~~~~~~~~~
-- [Link]
-- [Link]
-- [Link]
-- [Link]
-- [Link]
-----------------------------------------------------------------------------------
-----------------------------------------------
-- DISTINCT
-- it will not show the duplicate values
-- DISTINCT
-- it will not show the duplicate values
SELECT DISTINCT job_desc
FROM employee
ORDER BY job_desc;
-- function
-- block of code to perform specific task and it will run only when it is called.
-- syntax
-- function_name ()
-- function_name (argument1, argument2)
-- Aggregate Functions
~~~~~~~~~~~~~~~~~~~~~~~
-- COUNT()
-- AVG()
-- SUM()
-- MAX()
-- MIN()
-----------------------------------------------------------------------------
-- COUNT()
-- Count of
-- write query to get total no of employees whose salary is greater than 30000
-- show column heading as EMP_SALARY_MORE_THAN_30000
SELECT COUNT(salary) AS EMP_SALARY_MORE_THAN_30000
FROM employee
WHERE salary > 30000;
-- Average
-- AVG()
-- Addition
-- SUM()
-- Minimum
-- MIN()
-----------------------------------------------------------------------------------
---------------------------------------
-- String Functions
~~~~~~~~~~~~~~~~~~~~
-- UCASE()
-- LCASE()
-- CHAR_LENGTH()
-- CONCAT()
-- FORMAT()
-- LEFT(string, n)
-- RIGHT(string, n)
-----------------------------------------------------------------------------------
----------------------------------
-- UCASE()
-- converts to uppercase
-- LCASE()
-- converts to lowercase
-- convert all job description using LCASE with DISTINCT and ORDER BY
SELECT DISTINCT LCASE(job_desc)
FROM employee
ORDER BY LCASE(job_desc);
-- CHAR _LENGTH()
-- counts the length of characters
SELECT CHAR_LENGTH("sql");
-- count the length of characters in each employee name and show in separate column
SELECT emp_name, CHAR_LENGTH(emp_name)
FROM employee
ORDER BY emp_name;
-- Concatenation
-- joining / combining one or more characters
-- joining words or strings together
-- CONCAT()
-- join "Rs." with salary column and show next to employee name
SELECT emp_name, CONCAT("Rs.", salary)
FROM employee
ORDER By emp_name;
-- with alias
SELECT emp_name, CONCAT("Rs.", salary) AS salary
FROM employee
ORDER By emp_name;
-- with alias
SELECT CONCAT("Mr. / Mrs.", emp_name) AS name, CONCAT("Rs.", salary) AS salary
FROM employee
ORDER By emp_name;
-- FORMAT()
-- LEFT(string, n)
-- from the given string take no of characters from the left
-- RIGHT(string, n)
-- from the given string take no of characters from the right
-----------------------------------------------------------------------------------
--------------------------------------------
-- DATE Functions
~~~~~~~~~~~~~~~~~~~
-- NOW()
-- CURDATE()
-- DATE()
-- DATE_FORMAT
-- DATEDIFF()
-- DATE_ADD()
DESCRIBE employee;
SELECT *
FROM employee
ORDER BY emp_name;
SELECT *
FROM employee
WHERE job_desc = 'MANAGER'
ORDER BY emp_name;
-- Date Functions
-- NOW()
-- returns current system date and time
SELECT NOW();
-- CURDATE()
-- returns current system date
SELECT CURDATE();
SELECT CURDATE() AS date;
-- DATE(given_date)
~~~~~~~~~~~~~~~~~~~~~~~~
-- DATE(NOW())
-- returns current system date
SELECT DATE(NOW());
SELECT DATE(NOW()) AS date;
-- DATE_FORMAT(date, custom_format)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- changes date to custom date format
-- GROUP BY
SELECT job_desc
FROM employee
GROUP BY job_desc;
-- HAVING
SHOW INDEX
FROM employee;
SHOW INDEX
FROM employee;
SHOW INDEX
FROM employee;
SHOW INDEX
FROM employee;
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-
-- ON DELETE
-----------------------------------------------------------------------------------
------------------------------------------------
-- ON DELETE SET NULL
~~~~~~~~~~~~~~~~~~~~~~~~
SHOW TABLES;
DESCRIBE employee;
SELECT *
FROM employee
ORDER BY emp_name, job_desc;
DELETE
FROM branch
WHERE branch_id = 101;
SELECT *
FROM employee
ORDER BY emp_name, job_desc;
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------
-- ON DELETE CASCADE
~~~~~~~~~~~~~~~~~~~~~
DESCRIBE employee;
DELETE
FROM branch
WHERE branch_id = 101;
SELECT *
FROM employee
ORDER BY emp_name, job_desc;
-----------------------------------------------------------------------------------
-------------------------------------------------------------
-- insert data of few employees without br_id
INSERT INTO employee
(emp_name, job_desc, salary)
VALUES
("MAHALAKSHMI", "TESTER", 40000),
("DURAIMURUGAN", "DATA ANALYST", 100000),
("POOVARASAN", "DEVELOPER", 200000);
-- JOIN
-- Join is used to combine rows from one or more tables
-- Rows are combined based on related columns between tables
-- Joins combine rows horizontally.
-- Types of Joins
~~~~~~~~~~~~~~~~~~~~
-- INNER JOIN / LEFT INNER JOIN
-- LEFT JOIN / LEFT OUTER JOIN
-- RIGHT JOIN / RIGHT OUTER JOIN
-- FULL OUTER JOIN
-- CROSS JOIN
-----------------------------------------------------------------------------------
--------------------------------------------
-- INNER JOIN / LEFT INNER JOIN
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- left table -- employee
-- right table -- branch
SELECT *
FROM employee
INNER JOIN branch
ON employee.br_id = branch.branch_id;
-----------------------------------------------------------------------------------
---------------------------------------
-- LEFT JOIN / LEFT OUTER JOIN
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- left table -- employee
-- right table -- branch
SELECT *
FROM employee
LEFT OUTER JOIN branch
ON employee.br_id = branch.branch_id;
-----------------------------------------------------------------------------------
---------------------------------------
-- RIGHT JOIN / RIGHT OUTER JOIN
-- left table -- employee
-- right table -- branch
SELECT *
FROM employee
RIGHT OUTER JOIN branch
ON employee.br_id = branch.branch_id;
-----------------------------------------------------------------------------------
---------------------------------
-- FULL OUTER JOIN
-- not supported in mysql server
-----------------------------------------------------------------------------------
---------------------------------
-- CROSS JOIN
-- left table -- employee
-- right table -- branch
SELECT *
FROM employee
CROSS JOIN branch;
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
-- UNION
~~~~~~~~~~~~~~~~
-- rows are combined vertically
DESCRIBE client;
SELECT *
FROM client
ORDER BY client_id;
SELECT *
FROM branch
ORDER BY branch_id;
-- UNION
-- return unique values
SELECT *
FROM client
UNION
SELECT *
FROM branch;
-- UNION ALL
-- return duplicate rows also
SELECT *
FROM client
UNION ALL
SELECT *
FROM branch;
-- sub query
-- nested queries
-- query within a query
-- query inside query
-- outer query ( inner query )
-- use the output of inner query as input for outer query
-- PROBLEM 1 :
~~~~~~~~~~~~~~~~~
-- return employee name whose salary is more than minimum salary
-- return employee name whose salary is more than 20000 (minimum salary)
SELECT emp_name
FROM employee
WHERE salary > 20000;
-- TASK 1
-----------------------------------------------------------------------------------
------------------------------
-- create table subject
-- PRIMARY KEY -- subject_id
+----------------------+------------------+-------+------+------------
+-------------------------------------+
| Field | Type | Null | Key | Default | Extra
|
+----------------------+-------------------+--------+------+-----------
+------------------------------------+
| subject_id | int | NO | PRI | NULL |
auto_increment |
| subject_name | varchar(50) | YES | UNI| NULL |
|
+----------------------+-------------------+--------+-----+------------
+------------------------------------+
-- SOLUTION:
CREATE TABLE subject(
subject_id INT PRIMARY KEY AUTO_INCREMENT,
subject_name VARCHAR(50) UNIQUE
);
DESCRIBE subject;
-------------------------------------------------------------------------
-- TASK 2
-- create table customer
-- PRIMARY KEY -- customer_id
+-------------------------+------------------+-------+-------+---------
+-----------------------------+
| Field | Type | Null | Key | Default | Extra
|
+-------------------------+-------------------+-------+-------+----------
+----------------------------+
| customer_id | int | NO | PRI | NULL |
auto_increment |
| customer_name | varchar(50) | YES | | NULL |
|
| customer_city | varchar(50) | YES | | NULL |
|
+--------------------------+-------------------+-------+-------+----------
+----------------------------+
-- SOLUTION:
CREATE TABLE customer (
customer_id INT PRIMARY KEY AUTO_INCREMENT,
customer_name VARCHAR(50),
customer_city VARCHAR(50)
);
DESCRIBE customer;
-----------------------------------------------------------------------------------
-----------------------------------------------
-- TASK 3
-- create table institute
-- PRIMARY KEY -- institue_id
-- FOREIGN KEY -- customer_id (customer table)
-- FOREIGN KEY -- subject_id (subject table)
+-----------------------+----------------+------+-------+---------
+------------------------+
| Field | Type | Null | Key | Default | Extra
|
+-----------------------+----------------+------+-------+---------
+------------------------+
| institute_id | int | NO | PRI | NULL | auto_increment
|
| customer_id | int | YES | MUL | NULL |
|
| subject_id | int | YES | MUL | NULL |
|
+------------------------+---------------+-------+-------+----------
+-----------------------+
-- SOLUTION 1:
CREATE TABLE institute (
institute_id INT PRIMARY KEY AUTO_INCREMENT,
customer_id INT,
subject_id INT,
CONSTRAINT cat FOREIGN KEY(customer_id) REFERENCES customer(customer_id),
CONSTRAINT dog FOREIGN KEY(subject_id) REFERENCES subject(subject_id)
);
DESCRIBE institute;
-- SOLUTION 2:
CREATE TABLE institute (
institute_id INT PRIMARY KEY AUTO_INCREMENT,
customer_id INT,
FOREIGN KEY(customer_id) REFERENCES customer(customer_id),
subject_id INT,
FOREIGN KEY(subject_id) REFERENCES subject(subject_id)
);
DESCRIBE institute;
-----------------------------------------------------------------------------------
-----------------------------------------------------
-- EXISTS
~~~~~~~~~~~~~~
-- used with sub-query
PROBLEM:
~~~~~~~~~~~~
-- find the details of branches containing atleast one manager
SELECT *
FROM employee, branch
WHERE job_desc = "MANAGER"
AND
employee.br_id = branch.branch_id;
ANSWER:
~~~~~~~~~~~~~~~~~
SELECT branch_id, branch_name
FROM branch
WHERE EXISTS (
SELECT *
FROM employee
WHERE job_desc = "MANAGER"
AND
employee.br_id = branch.branch_id
);
-----------------------------------------------------------------------------------
---------------------------------------
-- ANY
~~~~~~~~~~~~
-- PROBLEM:
~~~~~~~~~~~~~~
-- write a query to find info of branches in which any employee gets salary more
than 50000
ANSWER:
~~~~~~~~~~~~~~~~~~~
SELECT *
FROM branch
WHERE branch_id = ANY (
SELECT br_id
FROM employee
WHERE salary > 50000
);
-----------------------------------------------------------------------------------
------------------------------
-- ALL
~~~~~~~~~~~~~
-- PROBLEM:
~~~~~~~~~~~~~~~~~~
-- write a query to find details of employees who are not working in mysore and
davangere
ANSWER 1:
~~~~~~~~~~~~~~~~
SELECT *
FROM employee
WHERE br_id <> ALL (
SELECT branch_id
FROM branch
WHERE branch_name IN ("mysore", "davangere")
);
ANSWER 2:
~~~~~~~~~~~~~~~~~~~~~~~~~~
SELECT *
FROM employee
WHERE br_id = ANY (
SELECT branch_id
FROM branch
WHERE branch_name NOT IN ("mysore", "davangere")
);
ANSWER 3:
~~~~~~~~~~~~~~~~~~~~~~~~~~
SELECT *
FROM employee
WHERE EXISTS (
SELECT branch_id
FROM branch
WHERE branch_name NOT IN ("mysore", "davangere")
AND
employee.br_id = branch.branch_id
);
ANSWER 4:
~~~~~~~~~~~~~~~~
SELECT *
FROM employee
WHERE br_id IN (
SELECT branch_id
FROM branch
WHERE branch_name NOT IN ("mysore", "davangere")
);
--------------------------------------------------------------------------------
PROBLEM:
~~~~~~~~~~~~~~~~~
-- write a query to find branch info of employees whose job description is sales
SELECT *
FROM branch
WHERE branch_id = ANY (
SELECT br_id
FROM employee
WHERE job_desc = 'sales'
);
ANSWER 2:
~~~~~~~~~~~~~~~~~~~~~~~~~~
FROM branch
WHERE branch_id IN (
SELECT br_id
FROM employee
WHERE job_desc = 'sales'
);
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----
VIEW
~~~~~~
-- Cretae View FISH to return left outer join of employee and branch
SELECT *
FROM fish;
Stored Procedure
~~~~~~~~~~~~~~~
A stored procedure is a prepared SQL code that you can save, so the code can be
reused over and over again.
Delimiter $$