The Wayback Machine - https://web.archive.org/web/20250427030200/https://www.geeksforgeeks.org/sql-select-database/
Open In App

SQL Select Database

Last Updated : 27 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

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

idnameagedepartmentsalary
1Alice30Sales50000
2Bob40Marketing60000
3Charlie35Sales55000
4David28HR45000
5Eve45Marketing65000
6Frank50HR70000
7Grace29IT48000
8Hannah38IT53000

1. Basic SELECT Statement

SELECT * FROM employees;

Output:

idnameagedepartmentsalary
1Alice30Sales50000
2Bob40Marketing60000
3Charlie35Sales55000
4David28HR45000
5Eve45Marketing65000
6Frank50HR70000
7Grace29IT48000
8Hannah38IT53000

Explanation: Retrieves all columns and all rows from the employees table.

2. Selecting Specific Columns

SELECT name, age FROM employees;

Output:

nameage
Alice30
Bob40
Charlie35
David28
Eve45
Frank50
Grace29
Hannah38

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:

nameage
Bob40
Charlie35
Eve45
Frank50
Hannah38

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:

nameage
Frank50
Eve45
Bob40
Hannah38
Charlie35
Alice30
Grace29
David28

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:

namesalary
Frank70000
Eve65000
Bob60000

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:

departmentaverage_salary
Sales52500
Marketing62500
HR57500
IT50500

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.


Next Article
Article Tags :

Similar Reads