Lesson3b-MY SQL
Lesson3b-MY SQL
Example:
Create database mydata;
SYNTAX:
Drop database database_name
Example:
Drop database mydata;
SYNTAX:
Drop database database_name
Example:
Drop database mydata;
SYNTAX:
Drop table table_name
Example:
Drop table items;
SYNTAX:
Drop table table_name
Example:
Drop table items;
SYNTAX:
Update table_name set column_name
= value, …
where condition;
Example:
Update items set Dep_id=101, salary=55000
where E_id = 7;
SYNTAX:
Delete from table_name where
condition;
Example:
Delete from employees where E_id= 6;
Using IN operator in WHERE clause
Example:
▪ Delete from employees where E_id IN
(101,102);
DELETE ALL
Example:
▪ Delete from employees;
Same
with delete statement with no
WHERE condition included
SYNTAX:
TRUNCATE table table_name
Example:
▪ TRUNCATE table employees;
Adding a new column
SYNTAX
ALTER table table_name
Add column_name datatype;
Example:
▪ Alter table employees add contact_no int(30);
Delete a column
SYNTAX:
ALTER table table_name
DROP column_name ;
Example:
▪ Alter table employees drop contact_no ;
Change a column name, data type
SYNTAX:
ALTER table table_name
ALTER column_name datatype ;
Example:
▪ Alter table employees alter column
contact_no int(30);
Rename database
SYNTAX:
RENAME database_name to
new_database_name
Example:
▪ RENAME db_info to db_studentinfo;
Rename statement to change several
tables
SYNTAX:
RENAME table_name to new_table_name,
table_name to new table_name;
Example:
▪ RENAME tb_stud to tb_studentinfo,
tb_studentinfo to tb_studs;
Toshow columns and properties of a
table.
SYNTAX:
DESCRIBE table_name;
SHOW COLUMNS from table_name;
Example:
▪ DESCRIBE employees;
Synonymous to DESCRIBE.
SYNTAX:
EXPLAIN table_name;
Example:
▪ EXPLAIN employees;
AVG() returns the average value
COUNT() returns the count of the
number of rows
returned
SUM() returns the sum
MAX() returns the maximum value
MIN() returns the minimum value
STD() returns the population
standard deviation
VARIANCE() Use the population
standard variance
SYNTAX:
Select AVG(column_name) from
table_name;
Example:
▪ Select AVG(Salary) from employee;
SYNTAX:
Select Count(column_name) from
table_name;
Example:
▪ Select Count(E_id) from employee;
▪ Select Count(*) from employee;
▪ Select Count(Distinct Salary) from
employee
SYNTAX:
Select Sum(column_name) from
table_name;
Example:
▪ Select Sum(Order_price) from employee;
▪ Select Sum(Distinct order) from
employee;
SYNTAX:
Select Max(column_name) from
table_name;
Example:
▪ Select Max(Order_price) as higher_order
from employee;
▪ Select Max(date_order) as recent_order from
employee;
▪ Select Max(customer_name) from employee;
SYNTAX:
Select Min(column_name) from
table_name;
Example:
▪ Select Min(Order_price) as higher_order
from employee;
▪ Select Min(date_order) as oldest_order from
employee;
▪ Select Min(customer_name) from employee;
STDDEV_POP() returns the
population
standard deviation
STDDEV_SAMP() returns the
sample
standard deviation
Example:
Select STDDEV_POP(order_price) as
‘Population Variance of Order prices’
from orders;
Example:
Select STDDEV_SAMP(order_price) as
‘Sample Variance of Order prices’ from
orders;
VAR_POP() returns the population
standard
variance
VAR_SAMP() returns the sample
variance
Example:
Select VAR_POP(order_price) as
‘Population Variance :Order prices’ from
orders;
Example:
Select VAR_SAMP(order_price) as
‘Sample Variance :Order prices’ from
orders;
UCASE() -converts field to upper case
LCASE() -converts field to lower case
MID() -returns a substring starting
from the specified position
CHAR_LENGHT -return a number of
characters in argument
ROUND() -rounds a numeric field to the
number of decimals specified
NOW() - returns the current system date
and time
FORMAT() - formats how a field is to be displaye
Format to uppercase
SYNTAX:
Select UCASE(column_name) from
table_name;
Example:
▪ Select order_id,UCASE(customer_name),
order_price from orders;
Format to lowercase
SYNTAX:
Select LCASE(column_name) from
table_name;
Example:
▪ Select order_id,LCASE(customer_name),
order_price from orders;
Allow users to extract characters from a
textfield by specifying the start position and
the length of the string you want to display.
SYNTAX:
Select MID(column_name,start,length)
from table_name;
Example:
▪ Select order_id,MID(customer_name,1,7)
from orders;
Count the number of characters in a
existing row.
SYNTAX:
Select CHAR_LENGTH(column_name)
from table_name;
Example:
▪ Select CHAR_LENGTH(customer_name) as
‘Length’ from orders;
Round numeric fields into a specific decimal
SYNTAX:
Select ROUND(column_name,decimals)
from table_name;
Example:
▪ Select ROUND(order_price,1) from orders;
▪ Select ROUND(-order_price,1) from
orders;
Get the current date and time of a
particular time zone
SYNTAX:
Select NOW(column_name,decimals)
from DUAL;
Select NOW();
An SQL Join clause is used to combine
rows from two or more tables, based on a
common field between them.
INNER JOIN
LEFT JOIN
RIGHT JOIN
FULL OUTER JOIN
UNION, UNION ALL
Return all rows from multiple tables where
the join condition is met.
Orders
OrderID CustomerID OrderDate
10308 1 2015-04-20
10309 37 2015-01-25
10310 2 2014-12-25
Customers
CustomerID CustomerName ContactName Country
1 Xiao Lu X. Lu Beijing
Note:
Inner join selects all rows from both tables as long
as there is a match between the columns. If there
are rows in the table that do not have matches in
the other table, it will not be listed.
Return all rows from left table (table 1), with
the matching rows in the right table(table 2).
The result is null in the right side when there
is no match.
SYNTAX:
Select column_name from table1
left join table2 on
table1.column_name=table2.column
_name;
Return all rows from left table (table 1), with
the matching rows in the right table(table 2).
The result is null in the right side when there
is no match.
SYNTAX:
Select column_name from table1
left join table2 on
table1.column_name=table2.column
_name;
Example:
SELECT Customers.CustomerName,
Orders.OrderID FROM Customers
LEFT JOIN Orders ON
Customers.CustomerID=Orders.Cust
omerID ORDER BY
Customers.CustomerName;
Return all rows from the right table (table
2), with the matching rows in the left
table(table 1). The result is null in the left
side when there is no match.
SYNTAX:
Select column_name from table1
right join table2 on
table1.column_name=table2.column
_name;
Example:
SELECT Customers.CustomerName,
Orders.OrderID FROM Customers
RIGHT JOIN Orders ON
Customers.CustomerID=Orders.Cust
omerID ORDER BY
Customers.CustomerName;
Used to combine the result-set of two or more
SELECT statements
Each SELECT statement within the union must
have the same number of columns. The columns
must also have similar data types. Also the
columns in each select statement must be in the
same order.
Syntax:
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
Note: The UNION operator selects only
distinct values by default. To allow duplicate
values, use the ALL keyword with UNION.
Example:
select customerId from customers
union select customerID from orders;