- SQL Tutorial
- SQL - Home
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Database
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Comments
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
SQL - SELECT Database, USE Statement
To work with a database in SQL, we need to first select the database we want to work with using the USE statement. The USE statement in SQL is used to specify the name of the database we want to select. After selecting the database, we can perform various operations on it such as creating tables, inserting data, updating data, and deleting data.
To retrieve the data from the database, we can use the SELECT statement in SQL, which is used to retrieve data from one or more tables.
Syntax
Following is the syntax of the USE statement in SQL −
USE DatabaseName;
Here, the “DatabaseName” is the name of the database that we want to create.
Always the database name should be unique within the RDBMS.
Example
First of all we are creating a database in the database system using the following query −
SQL> CREATE DATABASE testDB;
We can list all the available databases using the SELECT statement as shown below −
SQL> SELECT * FROM SYS.DATABASES; +------------+ | Database | +------------+ | master | | tempdb | | model | | msdb | | testDB | +------------+
Following query is used to select/switch the current database to <testDB> −
SQL> USE testDB;
Output
Commands completed successfully.
Once we finish switching to the database <testDB> we can perform operations such as creating a table as shown below −.
SQL> CREATE TABLE CALENDAR(MONTHS DATE NOT NULL);
Now, let us insert some records in the CALENDAR table using INSERT statements as shown in the query below −
SQL> INSERT INTO CALENDAR(MONTHS) VALUES('2023-01-01'); INSERT INTO CALENDAR(MONTHS) VALUES('2023-02-01'); INSERT INTO CALENDAR(MONTHS) VALUES('2023-03-01'); INSERT INTO CALENDAR(MONTHS) VALUES('2023-04-01'); INSERT INTO CALENDAR(MONTHS) VALUES('2023-05-01'); INSERT INTO CALENDAR(MONTHS) VALUES('2023-06-01'); INSERT INTO CALENDAR(MONTHS) VALUES('2023-07-01'); INSERT INTO CALENDAR(MONTHS) VALUES('2023-08-01'); INSERT INTO CALENDAR(MONTHS) VALUES('2023-09-01'); INSERT INTO CALENDAR(MONTHS) VALUES('2023-10-01'); INSERT INTO CALENDAR(MONTHS) VALUES('2023-11-01'); INSERT INTO CALENDAR(MONTHS) VALUES('2023-12-01');
Let’s verify whether the table is created or not using the SELECT statement as shown below −
SQL> SELECT * FROM CALENDAR;
Output
The table CALENDAR is successfully created.
+-------------+ | MONTHS | +-------------+ | 2023-01-01 | | 2023-02-01 | | 2023-03-01 | | 2023-04-01 | | 2023-05-01 | | 2023-06-01 | | 2023-07-01 | | 2023-08-01 | | 2023-09-01 | | 2023-10-01 | | 2023-11-01 | | 2023-12-01 | +-------------+
In SSMS
The following image shows that the table (CALENDAR) we created right now is stored inside the database(testDB) that we made as default database.
Selecting a non existent database
An attempt to select a non-existent database will result in an error. In the following query we are trying to switch to the database which does not exist −
SQL> CREATE DATABASE unknownDatabase;
On executing the above query, the output is displayed as follows −
Database 'unknownDatabase' does not exist. Make sure that the name is entered correctly.