SELECT

SELECT Syntax (Other Than SELECT *)

Create a lab environment:

mysql> CREATE DATABASE db1;
mysql> use db1;
mysql> CREATE TABLE tb (empid VARCHAR(10), sales INT, month INT);
mysql> INSERT INTO tb VALUES ('A103', 101, 4);
mysql> INSERT INTO tb VALUES ('A102', 54, 5);
mysql> INSERT INTO tb VALUES ('A104', 181, 4);
mysql> INSERT INTO tb VALUES ('A101', 184, 4);
mysql> INSERT INTO tb VALUES ('A103', 17, 5);
mysql> INSERT INTO tb VALUES ('A101', 300, 5);
mysql> INSERT INTO tb VALUES ('A102', 205, 6);
mysql> INSERT INTO tb VALUES ('A104', 93, 5);
mysql> INSERT INTO tb VALUES ('A103', 12, 6);
mysql> INSERT INTO tb VALUES ('A107', 87, 6);

Select columns in a specific order:

SELECT sales, empid FROM tb;

Alias for column names:

SELECT empid AS employee_id, sales AS sold_items FROM tb;

Arithmetics based on column value:

MySQL Functions

Functions for computation (names are self-explanatory):

Functions for displaying information (names are self-explanatory):

String Manipulation

Create a lab environment:

String concatenation:

Slice from the right:

Slice from the left:

Slice starting from the 2nd character of empid and get a substring of length 3:

Repeat for a variable times (used for drawing graphs):

Print a string reversely:

Date and Time

Get current time with NOW():

SELECT with Conditions

Limit the number of records:

Last updated

Was this helpful?