100 Must-Know SQL Commands

Listen to this Post

SQL is the backbone of data management, and mastering these commands will make you a database pro! Below is a detailed guide to essential SQL commands, along with practical examples and steps to help you practice and master them.

1️⃣ SELECT – Retrieve Data

Fetch data from a table.

SELECT * FROM employees; -- Get all columns 
SELECT name, age FROM employees; -- Get specific columns 

2️⃣ WHERE – Filter Data

Apply conditions to fetch specific data.

SELECT * FROM employees WHERE age > 30; 
SELECT * FROM employees WHERE department = 'IT'; 

3️⃣ ORDER BY – Sort Data

Sort results in ascending or descending order.

SELECT * FROM employees ORDER BY salary DESC; -- Highest salary first 

4️⃣ INSERT INTO – Add New Data

Insert new records into a table.

INSERT INTO employees (id, name, age, salary) 
VALUES (1, 'Alice', 30, 50000); 

5️⃣ UPDATE – Modify Existing Data

Change existing values in a table.

UPDATE employees SET salary = salary * 1.10 WHERE department = 'HR'; 

6️⃣ DELETE – Remove Data

Delete records from a table.

DELETE FROM employees WHERE age < 25; 

7️⃣ CREATE TABLE – Define Structure

Create a new table in the database.

CREATE TABLE employees ( 
id INT PRIMARY KEY, 
name VARCHAR(50), 
age INT, 
salary DECIMAL(10,2) 
); 

8️⃣ ALTER TABLE – Modify Table Structure

Add, modify, or remove columns.

ALTER TABLE employees ADD COLUMN department VARCHAR(50); 
ALTER TABLE employees DROP COLUMN age; 

9️⃣ JOINS – Combine Data from Tables

Retrieve related data from multiple tables.

SELECT employees.name, departments.dept_name 
FROM employees 
INNER JOIN departments ON employees.dept_id = departments.id; 

🔟 GROUP BY – Aggregate Data

Group data and perform calculations.

SELECT department, COUNT(*) FROM employees GROUP BY department; 

You Should Know:

To practice these SQL commands, you can use the following tools and environments:
1. MySQL: Install MySQL on your system and use the MySQL command-line client or MySQL Workbench.
2. SQLite: A lightweight database system that’s great for practice. Use the SQLite3 command-line tool.
3. Online SQL Editors: Websites like SQLFiddle or DB Fiddle allow you to write and test SQL queries online.

Example Setup for MySQL:

1. Install MySQL:

sudo apt-get update 
sudo apt-get install mysql-server 

2. Start MySQL:

sudo systemctl start mysql 

3. Access MySQL:

mysql -u root -p 

4. Create a database and table:

CREATE DATABASE practice_db; 
USE practice_db; 
CREATE TABLE employees ( 
id INT PRIMARY KEY, 
name VARCHAR(50), 
age INT, 
salary DECIMAL(10,2) 
); 

What Undercode Say:

Mastering SQL is a critical skill for anyone working with data. These commands form the foundation of database management and are essential for querying, updating, and organizing data efficiently. To further enhance your skills, explore advanced topics like subqueries, indexing, and stored procedures.

Additional Commands to Explore:

  • Indexing: Improve query performance.
    CREATE INDEX idx_name ON employees (name); 
    
  • Subqueries: Nested queries for complex data retrieval.
    SELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees); 
    
  • Stored Procedures: Reusable SQL code blocks.
    DELIMITER // 
    CREATE PROCEDURE GetEmployeeCount() 
    BEGIN 
    SELECT COUNT(*) FROM employees; 
    END // 
    DELIMITER ; 
    

Expected Output:

By practicing these commands and exploring advanced SQL features, you’ll be well-equipped to handle real-world database challenges. Whether you’re managing data for a small project or a large enterprise, SQL proficiency is a must-have skill in the IT and data-driven industries.

URLs for Further Learning:

References:

Reported By: Surajdubey Codes – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 TelegramFeatured Image