Structured Query Language (SQL) is the standard language used for managing and interacting with relational databases. Whether we are retrieving data, updating records, or defining the structure of our data, SQL commands provide a powerful and flexible way to handle these tasks.
This article will explain the basic SQL commands such as SELECT, INSERT, UPDATE, DELETE, and others, with examples and outputs to demonstrate their functionality. By mastering these commands, we can efficiently manage our database operations.
Basic SQL Commands
SQL commands are the building blocks of interacting with relational databases. These commands help in managing, retrieving, and manipulating data stored in structured formats. They are categorized into various types, such as Data Query Language (DQL), Data Manipulation Language (DML), and Data Definition Language (DDL). By understanding these commands, we can perform basic to advanced database operations efficiently.
We will use a sample table named employees to illustrate each command. This table represents an organization's employee data, including attributes such as ID, name, age, department, and salary. Below is the structure and data of the table
Employees TableNow, let's perform basic SQL commands on this table, explain their usage, and provide corresponding outputs.
1. SELECT Command
The SELECT command is used to retrieve specific data from one or more tables in a database. It allows us to choose particular columns, apply filters, and sort results as needed. This command forms the foundation of querying in SQL.
Example: Retrieve the name and salary of all employees.
The SELECT command retrieves data from one or more tables.
SELECT name, salary FROM employees;
Output
Select Command2. INSERT Command
The INSERT command is used to add new rows to a table. It lets us specify values for each column, either partially or completely, depending on the table's structure. This command is important for populating and maintaining data in tables.
Example: Add a new employee to the table.
The INSERT command adds new rows to a table.
INSERT INTO employees (id, name, age, department, salary)
VALUES (6, 'Emma Stone', 32, 'Marketing', 68000);
Output
Insert Command3. UPDATE Command
The UPDATE command modifies existing data in a table. By specifying a condition, we can update one or multiple rows. It is commonly used for correcting or altering record
Example: Update the salary of "John Doe".
The UPDATE command modifies existing records in a table.
UPDATE employees SET salary = 65000 WHERE name = 'John Doe';
Output
Update Command4. DELETE Command
The DELETE command removes rows from a table based on a specified condition. Unlike the DROP command, it does not delete the table structure, only the data within it.
Example: Remove "Jane Smith" from the table.
The DELETE command removes rows from a table.
DELETE FROM employees WHERE name = 'Jane Smith';
Output
Delete Command5. CREATE TABLE
The CREATE TABLE command is used to define a new table in the database. We specify the table name and its columns, along with their data types and constraints.
Example: Create a table for storing project information.
The CREATE TABLE command creates a new table named projects in database with columns for project_id, project_name, and budget.
CREATE TABLE projects (
project_id INT PRIMARY KEY,
project_name VARCHAR(50),
budget DECIMAL(10, 2)
);
6. DROP TABLE Command
The DROP TABLE command permanently deletes a table and its data from the database. It should be used with caution, as the operation cannot be undone.
Example: Delete the projects table.
The DROP TABLE command deletes an entire table and its data
DROP TABLE projects;
7. ALTER TABLE Command
The ALTER TABLE command modifies the structure of an existing table. It allows us to add, modify, or drop columns, as well as change constraints.
Example: Add a new column position to the employees table.
The ALTER TABLE command modifies the structure of a table.
ALTER TABLE employees ADD position VARCHAR(50);
Output
id | name | age | department | salary | position |
---|
1 | John Doe | 30 | IT | 65000 | NULL |
3 | Sam Brown | 35 | Finance | 75000 | NULL |
4 | Lisa White | 28 | IT | 62000 | NULL |
5 | Adam Green | 40 | HR | 80000 | NULL |
6 | Emma Stone | 32 | Marketing | 68000 | NULL |
8. WHERE Clause
The WHERE clause is used to filter records based on specific conditions. It works with most SQL commands, such as SELECT, UPDATE, and DELETE, to target only the relevant rows.
Example: Retrieve employees from the "IT" department.
The WHERE clause filters rows based on specific conditions.
SELECT name, department FROM employees WHERE department = 'IT';
Output
name | department |
---|
John Doe | IT |
Lisa White | IT |
9. ORDER BY Clause
The ORDER BY clause sorts the result set of a query in ascending or descending order based on one or more columns. This helps in organizing the output data effectively.
Example: Order employees by salary in descending order.
The ORDER BY clause sorts the rows in ascending or descending order. So, the employees are ordered by their salary
from highest to lowest.
SELECT name, salary FROM employees ORDER BY salary DESC;
Output
name | salary |
---|
Adam Green | 80000 |
Sam Brown | 75000 |
John Doe | 65000 |
Emma Stone | 68000 |
Lisa White | 62000 |
10. GROUP BY Clause
The GROUP BY clause groups rows that have the same values in specified columns into summary rows. It is often used with aggregate functions like COUNT, SUM, AVG, etc.
Example: Count the number of employees in each department.
The GROUP BY clause groups rows based on a column and performs aggregate functions.
SELECT department, COUNT(*) AS num_employees FROM employees GROUP BY department;
Output
department | num_employees |
---|
IT | 2 |
Finance | 1 |
HR | 1 |
Marketing | 1 |
Conclusion
Mastering SQL commands is essential for anyone working with databases. From basic data retrieval using SELECT
to modifying the structure of tables with ALTER TABLE
, SQL provides the foundation for managing data efficiently. By practicing these fundamental commands, we will be well-equipped to interact with any relational database and perform various operations to meet our data needs.
What is the purpose of the SELECT
statement in SQL?
The SELECT
statement is used to retrieve data from one or more tables in a database. You can specify which columns to display, filter data with the WHERE
clause, and order the results using ORDER BY
.
How does the GROUP BY
clause work in SQL?
The GROUP BY
clause is used to group rows that have the same values in specified columns. It is often used with aggregate functions like COUNT
, SUM
, AVG
, etc., to perform calculations on each group.
What is the difference between DELETE
and DROP
commands in SQL?
The DELETE
command is used to remove rows from a table, whereas the DROP
command is used to delete entire tables or databases. DELETE
keeps the table structure intact, while DROP
removes the table structure along with its data.
Similar Reads
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial Structured Query Language (SQL) is the standard language used to interact with relational databases. Whether you want to create, delete, update or read data, SQL provides the structure and commands to perform these operations. SQL is widely supported across various database systems like MySQL, Oracl
8 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
5 min read
SQL Query Interview Questions SQL or Structured Query Language, is the standard language for managing and manipulating relational databases such as MySQL, Oracle, and PostgreSQL. It serves as a powerful tool for efficiently handling data whether retrieving specific data points, performing complex analysis, or modifying database
15+ min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
Window Functions in SQL SQL window functions are essential for advanced data analysis and database management. It is a type of function that allows us to perform calculations across a specific set of rows related to the current row. These calculations happen within a defined window of data and they are particularly useful
6 min read
SQL | WITH Clause SQL queries can sometimes be complex, especially when you need to deal with multiple nested subqueries, aggregations, and joins. This is where the SQL WITH clause also known as Common Table Expressions (CTEs) comes in to make life easier. The WITH Clause is a powerful tool that simplifies complex SQ
6 min read
SQL Exercises : SQL Practice with Solution for Beginners and Experienced SQL (Structured Query Language) is a powerful and flexible tool for managing and manipulating relational databases. Regardless of our experience level, practising SQL exercises is essential for improving our skills. Regular practice not only enhances our understanding of SQL concepts but also builds
15+ min read
SQL Cheat Sheet ( Basic to Advanced) Creating and managing databases in SQL involves various commands and concepts that handle the structuring, querying, and manipulation of data. In this guide, we will see a comprehensive cheat sheet for essential SQL operations, offering a practical reference for tasks ranging from database creation
15 min read