You are on page 1of 7

Module 9 - Data Manipulation Language SQL Commands in MYSQL

Insert Records

To insert new record in a table, you can use INSERT INTO with SET or INSERT
INTO with VALUES.

To insert records where all fields have values:

<< INSERT INTO STUDENTTB SET STUDENTNO=101, FIRSTNAME= “JUAN”,


LASTNAME=”DELA CRUZ”, AGE=52, ADDRESS=”Antipolo, City”,
GENDER=”FEMALE”;

<<INSERT INTO STUDENTTB VALUES(102, “John”, “Doe”, 28, “Binangonan, Rizal”,


“Male”);

To insert records where some values have null value:

<< INSERT INTO STUDENTTB SET STUDENTNO=103, FIRSTNAME=”Joan”,


LASTNAME=”Santos”, AGE=22, GENDER=”Female”;
<<INSERT INTO STUDENTTB (STUDENTNO, FIRSTNAME, LASTNAME, AGE,
ADDRESS) VALUES (104, “Bea”, “Cruz”, 21, “Teresa, Rizal”);

Update Record

To update record, use the UPDATE statement, UPDATE table_name SET which
columns to change WHERE condition.

Delete Record

To delete a record, use the DELETE statement, DELETE FROM table_name


WHERE condition.
Be careful in executing the DELETE statement without the WHERE condition, all
records will be deleted.
Retrieving a Record

To retrieve a record, use SELECT statement. SELECT what_columns FROM table


or tables WHERE condition.

Note: * (asterisk) means it will display all the columns.

WHERE CLAUSE

The WHERE clause is used to extract only those records that fulfill a specified
criterion.
Here are the operator that can be used in retrieving a record with WHERE clause.

• The AND operator displays a record if both the first condition and the second
condition is true.
• The OR operator displays a record if either the first condition or the second
condition is true.
• The ORDER BY keyword is used to sort the result-set by a specified column.
• The ORDER BY keyword sort the records in ascending order by default.
• If you want to sort the records in a descending order, you can use the DESC
keyword.

OR
• mysql> select name from student where major = 'BCB' OR major = 'CS';

COUNT (Count query results)


• mysql> select count(name) from student where major = 'BCB' OR major = 'CS';

ORDER BY (Sort query results)


• mysql> select name from student where major = 'BCB' OR major = 'CS‘ ORDER
BY name;
• mysql> select name from student where major = 'BCB' OR major = 'CS‘ ORDER
BY name DESC;
• mysql> select * from student where major = 'BCB' OR major = 'CS‘ ORDER BY
student_id ASC, name DESC

LIKE (Pattern matching)


• mysql> select name from student where name LIKE "J%";

DISTINCT (Remove duplicates)


• mysql> select major from student;
• mysql> select DISTINCT major from student;
TABLE JOIN

 It is used to retrieve records from multiple tables.

INNER JOIN

• The INNER JOIN keyword return rows when there is at least one match in both
tables.
• SQL INNER JOIN Syntax
– SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_namePS: INNER
JOIN is the same as JOIN.

LEFT JOIN

• 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).
• SQL LEFT JOIN Syntax
– SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

RIGHT JOIN

• The RIGHT JOIN keyword returns all the rows from the right table
(table_name2), even if there are no matches in the left table (table_name1).
• SQL RIGHT JOIN Syntax
– SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
For example, we have two tables, customertb and ordertb.

INNER JOIN

LEFT JOIN

RIGHT JOIN
Wildcard Characters in MySQL

The wildcards can also be used in combinations!

Here are some examples showing different LIKE operators with '%' and '_' wildcards:

You might also like