Sei sulla pagina 1di 5

Kenneth P.

Go
CSCI30

1.)Select:
The SELECT clause specifies the table columns that are retrieved. The FROM
clause specifies the tables accessed. The WHERE clause specifies which table rows are
used. The WHERE clause is optional; if missing, all table rows are used.

Examples:
SELECT descr, color FROM p
SELECT * FROM sp
SELECT empno,ename from emp
SELECT max(sal),min(sal),avg(sal) from emp
SELECT ename from emp where ename like `A%`

2.)Sql Inner Joint:


The SQL INNER JOIN clause tells the database to only return rows where
there is a match found between table1 and table2. An INNER JOIN is most often (but not
always) created between the primary key column of one table and the foreign key column
of another table.

Examples:

SELECT Employee.Name,
TrainingTaken.TrainingTitle,TrainingTaken.TrainingDate FROM Employee
INNER JOIN TrainingTaken ON
Employee.EmployeeID=TrainingTaken.EmployeeID

SELECT Employee.Name, TrainingTaken.TrainingTitle,


TrainingTaken.TrainingDate
FROM Employee JOIN TrainingTaken
WHERE Employee.EmployeeID = TrainingTaken.EmployeeID

SELECT Customers.FirstName, Customers.LastName, SUM(Sales.SaleAmount) AS


SalesPerCustomer
FROM Customers JOIN Sales
ON Customers.CustomerID = Sales.CustomerID
GROUP BY Customers.FirstName, Customers.LastName

SELECT * FROM Individual


INNER JOIN Publisher
ON Individual.IndividualId = Publisher.IndividualId
WHERE Individual.IndividualId = '2'

SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name

3.)Sql Outer Joint:


The SQL OUTER JOIN clause is a variation of the SQL JOIN clause enables a
SELECT statement to access more than one table. The JOIN clause controls how tables
are linked. It is a qualifier of the SQL FROM clause. This type of join returns all rows
from one table and only those rows from a secondary table where the joined fields are
equal (join condition is met).

SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date


from suppliers, orders where suppliers.supplier_id = orders.supplier_id(+);

SELECT A1.store_name, SUM(A2.Sales) SALES


FROM Geography A1, Store_Information A2
WHERE A1.store_name = A2.store_name (+)
GROUP BY A1.store_name

SELECT Manufacturer, ManufacturerWebsite, ManufacturerEmail, AVG(Price)


AS AvgPrice
FROM Manufacturer, Product
WHERE Manufacturer.ManufacturerID = Product.ManufacturerID
GROUP BY Manufacturer, ManufacturerWebsite, ManufacturerEmail

SELECT
Department.DepartmentID,Department.DepartmentName,Employee.EmployeeNa
me From Employee Outer Join Department on Employee.DepartmentID =
Department.DepartmentID

Select
Employee.EmployeeID,Employee.EmployeeName,Department.DepartmentName
From Employee LEFT OUTER JOIN Department on Employee.DepartmentID =
Department.DepartmentID

4.)Sql Left Joint :


The LEFT JOIN keyword returns all rows from the left table (table_name1), even
if there are no matches in the right table (table_name2).

SELECT * FROM orders LEFT JOIN inventory ON orders.product =


inventory.product

SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo


FROM Persons
LEFT JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName

SELECT *
FROM employee LEFT OUTER JOIN department
ON employee.DepartmentID = department.DepartmentID;

SELECT CustomerName, TransDate, TransAmt FROM Customer LEFT OUTER


JOIN Transaction ON Customer.CustomerID = Transaction.CustomerID;

5.)Sql Right Joint:


RIGHT JOIN is another method of JOIN we can use to join together tables, but
its behavior is slightly different. We still need to join the tables together based on a
conditional statement. The difference is that instead of returning ONLY rows where a join
occurs, SQL will list EVERY row that exists on the right side, (The JOINED table)

SELECT *
FROM orders
RIGHT JOIN inventory
ON orders.product = inventory.product

SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo


FROM Persons
RIGHT JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName

SELECT s.stu_id, s.stu_name, s.stu_class, l.lib_no


from stu_table as s Right JOIN lib_table as l
on l.stu_id = s.stu_id

SELECT Employee.Username, Project.ProjectName


FROM Employee RIGHT JOIN Project
ON Employee.EmployeeID = Project.EmployeeID;
6.)Sql Sum Statement :
The SUM function returns the summed value of an expression.

SELECT SUM(expression )
FROM tables
WHERE predicates;

SELECT SUM(salary) as "Total Salary"


FROM employees
WHERE salary > 25000;

SELECT SUM(DISTINCT salary) as "Total Salary"


FROM employees
WHERE salary > 25000;

SELECT SUM(income - expenses) as "Net Income"


FROM gl_transactions;

SELECT department, SUM(sales) as "Total sales"


FROM order_details
GROUP BY department;

7.)Subquery Sql Statement:


Subquery or Inner query or Nested query is a query in a query. A subquery is
usually added in the WHERE Clause of the sql statement. Most of the time, a subquery is
used when you know how to search for a value using a SELECT statement, but do not
know the exact value.

SELECT "column_name1"
FROM "table_name1"
WHERE "column_name2" [Comparison Operator]
(SELECT "column_name3"
FROM "table_name2"
WHERE [Condition])

SELECT SUM(Sales) FROM Store_Information


WHERE Store_name IN
(SELECT store_name FROM Geography
WHERE region_name = 'West')

SELECT id, first_name


FROM student_details
WHERE first_name IN (SELECT first_name
FROM student_details
WHERE subject= 'Science');
SELECT p.product_name, p.supplier_name, (select order_id from order_items
where product_id = 101) as order_id from product p where p.product_id = 101;

SELECT p.product_name FROM product p


WHERE p.product_id = (SELECT o.product_id FROM order_items o
WHERE o.product_id = p.product_id);

8.) Create Database Statement :

The CREATE DATABASE statement is used to create a database

CREATE DATABASE company_db


CREATE DATABASE my_db
CREATE DATABASE sarisari_store
CREATE DATABASE emp_db
CREATE DATABASE student_db

9.) Create Table Statement:


The CREATE TABLE statement is used to create a table in a database.

CREATE TABLE "table_name"("column 1" "data_type_for_column_1","column


2" "data_type_for_column_2",... )

CREATE TABLE customer(First_Name char(50),Last_Name char(50),Address


char(50) default 'Unknown',City char(50) default 'Mumbai',Country
char(25),Birth_Date date)

CREATE TABLE Persons(P_Id int,LastName varchar(255),FirstName


varchar(255),Address varchar(255),City varchar(255))

CREATE TABLE product ( product_nbr int NOT NULL,product_name


varchar(40)NOT NULL,product_status_code char(10) NOT NULL,start_date date
NULL,end_date date NULL,raw_material_cost decimal(12,2) NULL,primary key
(product_nbr))

CREATE TABLE suppliers ( supplier_id number(10) not null, supplier_name


varchar2(50) not null, contact_name varchar2(50) );

Potrebbero piacerti anche