UPDATE employee
SET salary = 1.15 * salary;
Please note that each employee has 15% of his/hers own salary.
UPDATE employee
SET salary = 1.15 * salary
WHERE (now()-interval 5 year) > hired;
Of course we can change only a single row if we want.
UPDATE employee
SET salary = 1.05 * salary
LIMIT 3;
DELETE FROM employee WHERE salary > 50000.00;
DELETE FROM employee WHERE salary < 10000.00 ORDER BY name LIMIT 2;
Starting from version 4.0.0 the multi table delete format is supported in MySQL. The general form for this format is:
DELETE t1, t2 FROM t1, t2, t3 WHERE t1.id = t2.id AND t2.id=t3.id;In this example we remove matching rows from tables t1 t2.
SELECT * FROM students;This query will bring all information from table students. If we are interested in only some particular attributes (not all of them), then we can specify what attributes we want to see:
SELECT id, lastname, firstname, dateofbirth FROM students;
SELECT firstname, lastname FROM students WHERE gpa > 3.5;The WHERE clause specifies the criteria used to select particular rows. Table below contains different types of operators, which can be used in WHERE clause:
| Operator | Description | Example |
|---|---|---|
| = | Tests whether two values are equal |
SELECT name FROM customers WHERE id=17; |
| > | Tests whether the first value is greater than the other |
SELECT name FROM employee WHERE salary > 45000.00 |
| < | Tests whether the first value is less than the other | SELECT name FROM employee WHERE salary < 45000.00 |
| >= | Tests whether the first value is greater than or equal to the other | SELECT name FROM employee WHERE salary >= 55000.00 |
| <= | Tests whether the first value is less than or equal to the other | SELECT name FROM employee WHERE salary <= 55000.00 |
| != <> |
Tests whether two values are not equal | SELECT lastname FROM students WHERE gpa != 2; |
| IS NULL | Tests whether field actually contains a value. Please note that you can not use ... WHERE retired = NULL | SELECT lastname FROM students WHERE graduated IS NULL; |
| IS NOT NULL | Tests whether field doesn't contain a value. Please note that you can not use ... WHERE retired <> NULL | SELECT lastname FROM students WHERE graduated IS NOT NULL; |
| BETWEEN | Tests whether a value is greater or equal to a minimum value and less than or equal to a maximum value |
SELECT lastname
FROM students
WHERE graduated IS NOT NULL
AND
gpa BETWEEN 2.5 AND 3.9; |
| IN | Tests whether a value is in a particular set | SELECT lastname FROM students WHERE MONTH(graduated) IN (10, 11, 12); |
| NOT IN | Tests whether a value is not in a particular set | SELECT lastname FROM students WHERE MONTH(graduated) IN (6, 7, 8); |
| LIKE | Checks whether a value matches a pattern using simple SQL pattern matching.
You can use two special symbols here:
|
SELECT lastname
FROM students
WHERE lastname LIKE 'Bo%'
OR
lastname LIKE '_o%'; |
| NOT LIKE | Checks whether a value does not match a pattern | SELECT lastname FROM students WHERE lastname NOT LIKE '_____'; |
| REGEXP [BINARY] | Checks whether a value matches a regular expression. This is much more powerful than simple match using LIKE. For more information see MySQL documentation | SELECT lastname FROM students WHERE lastname REGEXP BINARY 'L.*r'; |
SELECT substring(name, 1, instr(name, ' ')-1) FROM employee;and the result:
substring(name, 1, instr(name, ' ')-1) -------------------------------------- John Bob Annie Bob 4 row(s) selectedEverything works just fine. The only thing I'm not happy about is the name of the column I see in the output. It's hard to understand at a glance what substring(name, 1, instr(name, ' ')-1) means. We can easily avoid this kind of things using column aliases:
SELECT substring(name, 1, instr(name, ' ')-1) AS 'First name',
substring(name, instr(name, ' ')+1) AS 'Last name'
FROM employee;
In this case the output will be:
First name Last name ------------- ---------------- John Smith Bob Smith Annie Lancaster Bob Bimon 4 row(s) selectedYou can even use these aliases in ORDER BY and GROUP BY clauses:
SELECT name, 1.15*salary as newsalary FROM employee ORDER by newsalary DESC;
SELECT * FROM employee ORDER BY hired DESC, salary ASC;This query will print all information about all employees and the rows will be sorted by hiring date (in descending order), if several employees have the same hiring date, then they will be sorted by their salary (in ascending order). Please note that you can use aliases (assign in SELECT clause) he instead of real column names.
SELECT * FROM employee LIMIT 3;Using LIMIT with two arguments we can control at what record the query will start printing the rows and how many of it'll print. So, the next query will print 5 more records from the employee table starting from the fourth one:
SELECT * FROM employee LIMIT 3, 5;More exactly, it will print no more than 5 rows (how many depends on how many records you have in your table). This feature is very convenient for web output when you don't wont to put too many information on one page.
SELECT * FROM employee, department;Please note than this query produces too many extra rows because it performs cartesian product. To avoid that we need to specify that we want to join these tables. The JOIN methods will be discussed later for now it's enough to specify additional restriction on printer rows:
SELECT * FROM employee, department WHERE id = depid;Unfortunately this query does not work. Why?
SELECT * FROM employee, department WHERE department.id = depid;
As a matter of fact sometime we need to select from two copies of the same table. Let's consider the following example:
SELECT name, salary
FROM employee, employee
WHERE name LIKE 'John%'
AND
salary > salary;
Obviously, this query will not work. Why ?
SELECT e2.name, e2.salary
FROM employee e1, employee e2
WHERE e1.name LIKE 'John%'
AND
e2.salary > e1.salary;
Some time we need to select from more than two tables. For example:
SELECT * FROM department JOIN employee WHERE department.id = employee.depid;Notice, we are using key word JOIN instead of comma. In this example only the rows where there was a match between the tables were included. Sometimes we especially interested in the rows where there is no match -- for example, departments that have no employees, books that have never been ordered, customers who never placed an order. The easiest way to answer this type of question in MySQL is to use a left join:
SELECT d.id, d.name, e.name
FROM department d LEFT JOIN employee e
ON d.id = e.depid;
Please note the difference :
| Left join | Cross join (usual) |
|---|---|
SELECT d.id, d.name, e.name
FROM department d LEFT JOIN employee e
ON d.id = e.depid;
|
SELECT d.id, d.name, e.name FROM department d, employee e WHERE d.id = e.depid; |
id name name SY Systems John Smith SY Systems Bob Smith SY Systems Bob Bimon SY Systems Tom Jeckson PH Physics Anny Lancaster PH Physics Sam Tinker NW New department NULL 7 row(s) selected |
id name name SY Systems John Smith SY Systems Bob Smith PH Physics Anny Lancaster SY Systems Bob Bimon SY Systems Tom Jeckson PH Physics Sam Tinker 6 row(s) selected |
SELECT department.id, department.name, 'Has no employee' AS Employment
FROM department LEFT JOIN employee
ON employee.depid = department.id
WHERE employee.id IS NULL;
Look at the difference between department LEFT JOIN employee
and employee LEFT JOIN department.
| Name | Description |
|---|---|
| Average of values in the specified column | |
| COUNT(items) | If you specify a column, this will give you the number of non-NULL values on that column. If you add the word DISTINCT in front of the column name, you will get a count of the distinct values in that column only. If you specify COUNT(*), you will get a row count regardless of NULL values. |
| MIN(column) | Minimum of values in the specified column |
| MAX(column) | Maximum of values in the specified column |
| STD(column) | Standard deviation of values in the specified column |
| Same as STD(column) | |
| SUM(column) | Sum of values in the specified column |
SELECT COUNT(*) FROM employee;
SELECT depid, COUNT(id) FROM employee GROUP BY depid;
SELECT D.name, COUNT(E.id), AVG(salary)
FROM department D LEFT JOIN employee E
ON D.id = E.depid
GROUP BY D.name;
SELECT D.name,
COUNT(E.id) AS 'Emp #',
TRUNCATE(IFNULL(AVG(salary), 0), 2) AS 'Average Salary'
FROM department D LEFT JOIN employee E
ON D.id = E.depid
GROUP BY D.name;
SELECT D.name, COUNT(E.id) as empnum FROM department D, employee E WHERE D.id = E.depid GROUP BY D.name HAVING empnum > 2;
SELECT AVG(salary) FROM employee;and we know how to find all department with the average salary greater than a specified value:
SELECT D.name FROM department D, employee E WHERE D.id = E.depid GROUP BY D.name HAVING AVG(salary) > specified_value;What we can not do is to substitute the first query for that specified_value into the second one. So, we need a way to remember the result of the first query and use it in the second one. MySQL offers a way to do it. This way uses user variables. We need to run these two queries to achieve the goal:
SELECT @avg_salary:=avg(salary)
FROM employee;
SELECT D.name AS Department,
truncate(avg(salary),2) AS ASalary
FROM department D, employee E
WHERE D.id = E.depid
GROUP BY D.name
HAVING ASalary > @avg_salary;