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 DATABASE statement is not a standard SQL command, but rather a variation of the USE command 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.
Query:
USE database_name;
SQL Select Database
-
The SELECT statement in SQL is used to query and retrieve data from a database.
-
It specifies which columns of data we want to retrieve and from which tables.
-
However, if we are asking about “selecting” a database, we might be referring to how to choose or switch between databases in SQL.
Syntax to Select a Database in SQL
The syntax to select a database in SQL is:
USE DATABASE_NAME;
Example of SQL Select Database
Let’s go through each SQL query and show the expected output based on the provided employees table.
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
Query:
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
Query:
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
Query:
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
Query:
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
Query:
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
Query:
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.