MariaDB (and SQL) select command

select command is a SQL standard command, and is used to retrieve information from database tables.
If you want to get all rows and columns from specific tables,then it is very simple:
SELECT * FROM <tableName>;
For example if we want to see all row in the employee table:


MariaDB [firstDB]> SELECT * FROM employee;
+------------+-----------+-----------------+--------+
| first_name | last_name | employee_number | salary |
+------------+-----------+-----------------+--------+
| Julia      | Roberts   |               1 |  50000 |
| Edison     | Ford      |               2 |  45000 |
| Irina      | Shayk     |               3 |  60000 |
| Yigal      | Arnon     |               4 |  29000 |
+------------+-----------+-----------------+--------+
4 rows in set (0.00 sec)

And we can see the 4 rows we have inserted before.

If we want to select only some columns, then we should write their name from the “FROM”, and separate them with comma.
The structure will be the following:
SELECT <column1>,<column2>... FROM <tableName>;

For example if we want to select only last_name and salary:


MariaDB [firstDB]> SELECT last_name, salary FROM employee;
+-----------+--------+
| last_name | salary |
+-----------+--------+
| Roberts   |  50000 |
| Ford      |  45000 |
| Shayk     |  60000 |
| Arnon     |  29000 |
+-----------+--------+
4 rows in set (0.00 sec)

If we want to select first_name only:


MariaDB [firstDB]> SELECT first_name FROM employee;
+------------+
| first_name |
+------------+
| Julia      |
| Edison     |
| Irina      |
| Yigal      |
+------------+
4 rows in set (0.00 sec)


If we want first_name, salary and employee_number:


MariaDB [firstDB]> SELECT first_name, salary, employee_number FROM employee;
+------------+--------+-----------------+
| first_name | salary | employee_number |
+------------+--------+-----------------+
| Julia      |  50000 |               1 |
| Edison     |  45000 |               2 |
| Irina      |  60000 |               3 |
| Yigal      |  29000 |               4 |
+------------+--------+-----------------+
4 rows in set (0.01 sec)

If we want to select only some rows that match a condition, then we can use the “WHERE” option:
SELECT * FROM WHERE <condtion>; 
So for example, if we want to see only employee that earn more than 45,000:


MariaDB [firstDB]> SELECT * FROM employee WHERE salary > 45000;
+------------+-----------+-----------------+--------+
| first_name | last_name | employee_number | salary |
+------------+-----------+-----------------+--------+
| Julia      | Roberts   |               1 |  50000 |
| Irina      | Shayk     |               3 |  60000 |
+------------+-----------+-----------------+--------+
2 rows in set (0.01 sec)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s