Overview of SQL and Tips

mySQL basics including format of statements and comments etc.

mySQL Syntax

SQL statements are not case sensitive. This means the following would be considered the same.

select * from table;
SELECT * FROM  table;

Note, table names can be case sensitive depending on the OS of the server where the database is located. Since this information may not be avialable it is recommended to use the same case for the table as originally named.

Some databases require semicolons after their statements while others don’t. That being said it is recommended and good practice to have a semicolon after each statement.

Comments

Using “– “ allows you to make comments inline with a statement. These comments look like the following.

select * from table; -- this is an inline comment

Whenyou want to make a longer comment that can take up more than one line you can do the following.

/*
    this is a 
    multi-line comment
*/
select * from table;

SQL Clauses

A statement may have more than one clauses as seen in the following statement.

select * from album where label = 'America';

SQL Functions

Functions are used to perform specific operations on data. The count function is used to find the numbers of rows which match the condition in the where clause in the example.

select count{*) from album where label = 'America';

SQL Expressions

Expressions are used in SQL to derive values from data. The example statement has 2 expressions. One expression divides the polulation column by one million in order to display the polulation in millions. The second is a logical expression that is used to select only those rows where the population column is greater than or equal to one million.

select name, population / 1000000 as PopMM
from country
where polulation >= 1000000
order by polulation desc;
Tags: mysql
Share: Twitter Facebook LinkedIn