SELECT Statement in MariaDB
Last Updated :
01 Dec, 2023
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.
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:
Select StatementQuery to Fetch the Specific Columns From the Table Employees
Query:
SELECT first_name, last_name FROM employees;
Output:
Select StatementSelect 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 with WHERE Clause
If we want to see the values in table with the specific conditions then we will use WHERE Clause with SELECT statement.
Query:
SELECT * FROM employees WHERE department_id = 2;
This retrieves all the columns from the "employees" table where the department ID is 2.
Output:
Select StatementORDER Data with ORDER BY Clause
Query:
SELECT * FROM employees ORDER BY salary DESC;
In order to arrange the results by salary in descending order, this fetches all columns from the "employees" table.
Output:
Select StatementAggregate Function
Query:
SELECT AVG(salary) AS average_salary FROM employees;
Calculates the average salary from the employees table.
Output:
Select StatementGrouping Data
Query:
SELECT department_id, COUNT(*) AS employee_count FROM employees GROUP BY department_id;
Count the number of employees in each department by grouping the data according to the department ID.
Output:
Select StatementConclusion
MariaDB's strong and flexible SELECT statements allow users to access and modify data precisely. In order to effectively query databases and analyze data we have to first understand syntax and its other characteristics. It will definitely help you deepen your understanding of SELECT statements as you learn more about MariaDB. This article shows how to query data from a table using basic MariaDB SELECT statements.
Similar Reads
What is Nested Select Statement in MariaDB
Nested select statements, normally named subqueries, represent an advanced feature for MariaDB which enables more efficient and thorough querying of the data. Therefore, the nesting of a SELECT statement within another allows us to perform operations and filtering that could be hardly possible with
4 min read
MariaDB INSERT Statement
MariaDB is a famous open-source relational database management system. It is renowned for its performance, scalability, and robust functions. One essential thing about operating with databases is the ability to insert information, and the INSERT statement plays a crucial role in this technique. In t
3 min read
Nested Select Statement in MySQL
The relational databases, the ability to retrieve and manipulate data with precision is a cornerstone of effective database management. MySQL, a popular relational database management system, provides a powerful tool called the Nested SELECT statement that empowers developers to perform complex and
5 min read
MySQL SELECT Statement
The MySQL SELECT statement is essential for fetching data from tables. It retrieves information and stores it in a result table, often referred to as a result set. Widely used in MySQL, SELECT is a fundamental command for querying databases. This article covers the basics of SELECT syntax and explor
4 min read
SQL SELECT INTO Statement
The SELECT INTO statement in SQL is a powerful and efficient command that allow users to create a new table and populate it with data from an existing table or query result in a single step. This feature is especially useful for creating backups, extracting specific subsets of data, or preparing new
5 min read
Truncate Table in MariaDB
MariaDB is an open-source relational database management system. It offers faster query execution and data manipulation as compared to other RDBMS. Also, it handles large datasets very efficiently. MariaDB Provides plugins for various functionalities like backup, performance monitoring, security, an
4 min read
MySQL INSERT INTO SELECT Statement
MySQL is an open-source relational database management system that uses Structured Query Language (SQL) to manipulate databases. It stores data in a table format. It provides various statements to perform Create, Read, Update, and Delete operations on a database table. INSERT INTO SELECT statement i
5 min read
SET Variable in MariaDB
In MariaDB, the SET statement is a main tool in variable handling. Users can assign values to variables, operate with them, and control database operations in various respects. This article includes a look at the SET variable usage in MariaDB and its syntax together with some examples. SET Variable
4 min read
MariaDB UPDATE Statement
MariaDB uses SQL (Structured Query Language) and it is an open-source relational database management system (RDBMS) for managing and manipulating data. MariaDB is known for its high performance, even on large datasets. This makes it a good choice for applications that require fast data access. Maria
6 min read
Show Tables in MariaDB
MariaDB is an open-source relational database management system (RDBMS). MariaDB is a very successful RDBMS that is known for its performance, scalability, and ease of use. When dealing with databases, understanding the structure and organization of their table type becomes important. MariaDB provid
4 min read