--Select
SELECT home_id, home_type, bathrooms
FROM homes
WHERE home_id < 500
AND home_type = 'two-storey'
ORDER BY home_type ASC, bathrooms DESC;
--Sample2
SELECT *
FROM homes
WHERE bathrooms >= 2
ORDER BY home_type ASC;
--Another
SELECT * FROM contacts
WHERE last_name = 'Smith'
AND contact_id >= 1000
AND contact_id <= 2000;
--Between
SELECT *
FROM contacts
WHERE last_name = 'Smith'
AND contact_id BETWEEN 1000 AND 2000;
----------------------------------------------------------
--CREATE TABLE
CREATE TABLE suppliers
( supplier_id number(10) not null,
supplier_name varchar2(50) not null,
city varchar2(50),
CONSTRAINT suppliers_pk PRIMARY KEY (supplier_id)
);
INSERT INTO suppliers (supplier_id, supplier_name, city)
VALUES (5001, 'Microsoft', 'Chicago');
INSERT INTO suppliers (supplier_id, supplier_name, city)
VALUES (5002, 'IBM', 'Chicago');
INSERT INTO suppliers (supplier_id, supplier_name, city)
VALUES (5003, 'Red Hat', 'Detroit');
INSERT INTO suppliers (supplier_id, supplier_name, city)
VALUES (5004, 'NVIDIA', 'New York');
----------------------------------------------------------
--Delete
DELETE FROM customers;
DELETE FROM customers
WHERE last_name = 'Smith';
----------------------------------------------------------
--Update Data
--Multiple single column
UPDATE customers
SET last_name = 'Anderson'
WHERE customer_id = 5000;
--Multiple column
UPDATE customers
SET state = 'California',
customer_rep = 32
WHERE customer_id > 100;
--Update Data from another table
UPDATE customers
SET c_details = (SELECT contract_date
FROM suppliers
WHERE suppliers.supplier_name = customers.customer_name)
WHERE customer_id < 1000;
----------------------------------------------------------
--Insert
INSERT ALL
INTO suppliers (supplier_id, supplier_name) VALUES (1000, 'IBM')
INTO suppliers (supplier_id, supplier_name) VALUES (2000, 'Microsoft')
INTO suppliers (supplier_id, supplier_name) VALUES (3000, 'Google')
SELECT * FROM dual;
INSERT INTO suppliers
(supplier_id, supplier_name)
VALUES
(5000, 'Apple');
INSERT INTO suppliers
(supplier_id, supplier_name)
SELECT account_no, name
FROM customers
WHERE customer_id > 5000;
INSERT INTO clients
(client_id, client_name, client_type)
SELECT supplier_id, supplier_name, 'advertising'
FROM suppliers
WHERE NOT EXISTS (SELECT *
FROM clients
WHERE clients.client_id = suppliers.supplier_id);
----------------------------------------------------------
--Join Table
--SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date
--FROM suppliers
INNER JOIN orders
LEFT OUTER JOIN orders
RIGHT OUTER JOIN orders
FULL OUTER JOIN orders
--ON suppliers.supplier_id = orders.supplier_id;