SQL Basics Introduction
SQL, which stands for Structured Query Language, is used to communicate with relational databases. SQL Server uses SQL commands to create, retrieve, modify and manage data stored in databases.
What is SQL?
SQL is a standard language used to work with relational databases. It allows developers and database administrators to perform operations such as creating databases, creating tables, inserting records, retrieving data, updating records and deleting records.
Simple Definition
SQL is a language used to communicate with and manage data in a relational database.
Why Do We Use SQL?
SQL provides commands that allow us to work with the data stored inside database tables.
- Create databases and tables
- Insert new records
- Retrieve data
- Update existing records
- Delete records
- Filter and sort data
- Manage database objects
SQL and SQL Server
SQL is the language used to communicate with a relational database. SQL Server is a relational database management system developed by Microsoft that understands and executes SQL statements.
Application
↓
SQL Query
↓
SQL Server
↓
Database Engine
↓
Database
↓
Result
First SQL Query
A simple SELECT statement can be used to retrieve data from a table.
SELECT *
FROM Students;
The above query retrieves all columns and all records from the Students table.
Example
Suppose the Students table contains the following records:
| StudentId | StudentName | Age |
|---|---|---|
| 101 | Rahul | 22 |
| 102 | Priya | 21 |
| 103 | Amit | 23 |
SELECT StudentName, Age
FROM Students;
Output
| StudentName | Age |
|---|---|
| Rahul | 22 |
| Priya | 21 |
| Amit | 23 |
Basic SQL Statement Structure
Most SQL statements follow a specific structure depending on the operation being performed.
SELECT column_name
FROM table_name;
| Keyword | Purpose |
|---|---|
| SELECT | Specifies the columns to retrieve. |
| FROM | Specifies the table from which data is retrieved. |
Basic SQL Operations
SQL provides different commands for performing common database operations.
| Operation | SQL Command | Purpose |
|---|---|---|
| Create | CREATE | Create database objects. |
| Retrieve | SELECT | Retrieve data. |
| Insert | INSERT | Add new records. |
| Update | UPDATE | Modify existing records. |
| Delete | DELETE | Remove records. |
How a SQL Query Works
When a SQL query is executed, SQL Server processes the statement and returns the requested result.
User
↓
SQL Statement
↓
SQL Server
↓
Database Engine
↓
Table / Data
↓
Result
Simple SQL Examples
-- Retrieve data
SELECT * FROM Students;
-- Insert data
INSERT INTO Students
VALUES (104, 'Neha', 22);
-- Update data
UPDATE Students
SET Age = 23
WHERE StudentId = 104;
-- Delete data
DELETE FROM Students
WHERE StudentId = 104;
Important
SQL statements should always be written carefully, especially UPDATE and DELETE statements. A missing WHERE condition can affect multiple records.
Summary
In this section, we learned what SQL is, why SQL is used, how SQL communicates with SQL Server, the basic structure of SQL statements and the common operations used to work with database data.