Monday, August 13, 2012

Querying MySQL


SELECT - this term is used to return rows of records that meet certain criteria.
Command: SELECT whatcolumns FROM whattable

Example of SELECT QUERY
            SELECT name FROM student

ASTERISK
The asterisk (*) select all the data from the chosen table.
Command: SELECT * from student

We can list a set of columns we would like to return. You should specify the column names as a list of comma-separated values.
Command: SELECT column1, column2, FROM table_name

Basic form of the SQL SELECT statement is called a mapping or a SELECT-FROM-WHERE block.
SELECT column_name FROM table_name WHERE condition_statement.

You can used symbols in condition such as =, !=, < >, <, <=, >, >=. + and - sign is used in incrementing like age++ or age--.

AND, OR, NOT
            This is used in WHERE conditions. AND is used if you have one or more conditions, OR is used if you want to show only one. Not is used if you don’t want to show a particular data.
Command:
            SELECT column_name FROM table_name WHERE condition1 AND/OR/NOT condition2
Example:
            SELECT course FROM student WHERE degree=”BSIT” AND degree=”COMSCI”

LIKE
            This is pattern matching. This is used in text only. It used a % symbol.
Command:
            SELECT column_name FROM table_name WHERE column_name LIKE ‘%char’.
Example:
            SELECT name FROM student WHERE name LIKE ‘s%’.
                        -it means that all names start from the letter S will show.
            %s = all names that end in a letter s
            S%m = all names that starts in a letter S and ends with a letter m

AGGREGATE OPERATIONS

SUM() = this will sum all the value in the column.
            Ex. SELECT SUM(age) FROM student

AVERAGE = this will compute the average of all the value in the column.
            Ex. SELECT AVERAGE(age) FROM student

MIN() = it means minimum.
            Ex. SELECT MIN (age) FROM student
            - this command shows the youngest age in the table student.

MAX() = maximum
            Ex. SELECT MAX(age) FROM student
            - this command shows the oldest age in the table student.

COUNT()
            Ex. SELECT COUNT(age) FROM student
            - this will count all the number of age in a table student

DISTINCT
If you don’t want to show a duplicate data, you can use distinct. It can show the unique data
Command:
            SELECT DISTINCT(column_name) FROM table_name
Example:
            SELECT DISTINCT(degree) FROM student

GROUP BY
            This is for grouping a data in a selected data.
Command:
            SELECT * FROM table_name GROUP BY column_name
Example:
            SELECT * FROM student GROUP BY age

HAVING
            This is used in grouping data, this is accompanied by GROUP BY query.
Command:
            SELECT column_name FROM table_name GROUP BY column_name HAVING condition
Example:
            SELECT age FROM student GROUP BY age HAVING age>10

ORDER BY
            This is used in sorting data. You can use ASC in ascending order or desc in descending order.
Command:
            SELECT column_name FROM table_name ORDER BY column_name
Example:
            SELECT name FROM student ORDER BY age desc
- this command means that it will sort the name by age in descending order meaning oldest to youngest.

No comments:

Post a Comment