SELECT Statement
SELECT statement in MariaDB is used to data from one or more tables stored in a database. They include UNION statements, ORDER BY clauses, WHERE clauses, etc., and subqueries.
Syntax:
SELECT column1, column2, … FROM table_name [WHERE condition];
- SELECT Clause: It specifies the columns you want to retrieve from the table.
- FROM Clause: It specifies the table from which the data will be retrieved.
- WHERE Clause: It filters the rows based on specific conditions.
Let’s create a table of employees and insert some data into this.
Query for Create Table
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department_id INT,
salary DECIMAL(10, 2)
);
Query for Insert Data
INSERT INTO employees VALUES
(1, 'John', 'Doe', 3, 50000.00),
(2, 'Jane', 'Smith', 2, 60000.00),
(3, 'Bob', 'Johnson', 1, 75000.00),
(4, 'Alice', 'Williams', 2, 65000.00),
(5, 'Charlie', 'Brown', 1, 70000.00);
Query to Fetch all the Fields From the Table Employees
Query:
SELECT * FROM employees;
Output:
Query to Fetch the Specific Columns From the Table Employees
Query:
SELECT first_name, last_name FROM employees;
Output:
Select Columns From Multiple Tables
Query:
SELECT employees.employee_id, employees.first_name, products.product_name FROM employees
JOIN products ON employees.employee_id = products.employee_id;
If you want to select a specific column from employees table and products table, let’s say there is a common column called employee_id that joins these two tables we will use this type of query.
SELECT Statement in MariaDB
MariaDB uses SQL (Structured Query Language) and it is an open-source relational database management system (RDBMS) for managing and manipulating data. The SELECT statement is the most basic of all the SQL statements. It is essential to get data out of one or more database tables and display it. In this article, we will examine the syntax of the SELECT statement in MariaDB to retrieve records from a table.