SQL Select Database
The USE DATABASE statement is a command in certain SQL-based database management systems that allows users to select and set a specific database as the default for the current session. By selecting a database, subsequent queries are executed within the context of that database, making it easier to interact with tables and other objects contained within it.
Additionally, the SELECT statement in SQL is used to query and retrieve data from the tables within the selected database. In this article, We will learn about SQL Select Database by understanding various examples in detail and so on.
The USE DATABASE Statement
- The
USE DATABASEstatement is not a standard SQL command, but rather a variation of theUSEcommand used in some SQL database management systems (DBMS) to select a specific database for the current session. - This command sets the default database for subsequent queries in that session.
Syntax:
USE database_name;Example of SQL Select Database
Let’s take a look at how to select a database in SQL, using MySQL as an example. Suppose you have a database called company_db that contains employee information.
- Create a Database: To begin, you can create a new database if it doesn’t already exist
CREATE DATABASE GeeksforGeeks;- Select the Database: To set GeeksforGeeks as the active database, use the USE command
USE GeeksforGeeks;Once you’ve selected the database, any queries you execute will be performed within the context of GeeksforGeeks until you select another database.
How to Query Data from the Selected Database
The SELECT statement in SQL is used to query and retrieve data from the tables within the selected database. Here are some key ways to use the SELECT statement effectively
Table: employees
| id | name | age | department | salary |
|---|---|---|---|---|
| 1 | Alice | 30 | Sales | 50000 |
| 2 | Bob | 40 | Marketing | 60000 |
| 3 | Charlie | 35 | Sales | 55000 |
| 4 | David | 28 | HR | 45000 |
| 5 | Eve | 45 | Marketing | 65000 |
| 6 | Frank | 50 | HR | 70000 |
| 7 | Grace | 29 | IT | 48000 |
| 8 | Hannah | 38 | IT | 53000 |
1. Basic SELECT Statement
SELECT * FROM employees;Output:
| id | name | age | department | salary |
|---|---|---|---|---|
| 1 | Alice | 30 | Sales | 50000 |
| 2 | Bob | 40 | Marketing | 60000 |
| 3 | Charlie | 35 | Sales | 55000 |
| 4 | David | 28 | HR | 45000 |
| 5 | Eve | 45 | Marketing | 65000 |
| 6 | Frank | 50 | HR | 70000 |
| 7 | Grace | 29 | IT | 48000 |
| 8 | Hannah | 38 | IT | 53000 |
Explanation: Retrieves all columns and all rows from the employees table.
2. Selecting Specific Columns
SELECT name, age FROM employees;Output:
| name | age |
|---|---|
| Alice | 30 |
| Bob | 40 |
| Charlie | 35 |
| David | 28 |
| Eve | 45 |
| Frank | 50 |
| Grace | 29 |
| Hannah | 38 |
Explanation: Retrieves only the name and age columns for all rows.
3. Filtering Results with WHERE
SELECT name, age FROM employees WHERE age >= 35;Output:
| name | age |
|---|---|
| Bob | 40 |
| Charlie | 35 |
| Eve | 45 |
| Frank | 50 |
| Hannah | 38 |
Explanation: Retrieves names and ages of employees older than 35.
4. Sorting Results with ORDER BY
SELECT name, age FROM employees ORDER BY age DESC;Output:
| name | age |
|---|---|
| Frank | 50 |
| Eve | 45 |
| Bob | 40 |
| Hannah | 38 |
| Charlie | 35 |
| Alice | 30 |
| Grace | 29 |
| David | 28 |
Explanation: Retrieves names and ages of all employees sorted by age in descending order.
5. Limiting Results with LIMIT Clause
SELECT name, salary FROM employees ORDER BY salary DESC LIMIT 3;Output:
| name | salary |
|---|---|
| Frank | 70000 |
| Eve | 65000 |
| Bob | 60000 |
Explanation: Retrieves the top 3 highest-paid employees, ordered by salary in descending order.
6. Aggregating Data with GROUP BY and Aggregation Functions
SELECT department, AVG(salary) AS average_salary FROM employees GROUP BY department;Output:
| department | average_salary |
|---|---|
| Sales | 52500 |
| Marketing | 62500 |
| HR | 57500 |
| IT | 50500 |
Explanation: Calculates the average salary for each department.
Conclusion
The ORDER BY clause is an essential component in PL/SQL for controlling the order of query results. By using this clause, developers can enhance the readability and usefulness of their data, whether by arranging records by a single column or employing more advanced techniques like case-insensitive sorting or sorting by calculated columns.

