Introduction:
SQL, Stands for Structured Query Language. It is a language used to accessing the databases. It manages the database such as insert, retrieve, update, and delete data stored within relational databases like MySQL, SQL server. For beginners, SQL querying is the primary step to gaining solid confidence in the handling of data.
In this article, we will explore 10 basic SQL queries every programmer should know. These queries are widely used in practical applications and help you understand how to work effectively with databases.
1. SELECT: In SQL, this statement is used to fetch the data from a database.
Syntax:
SELECT column1, column2, ..., column N
FROM table_name;
Example:
SELECT * FROM person;
Explanation:
Above query fetches all the records (*) from the table Person So, if you want to get a specified name and salary from the table:
SELECT name, salary FROM person;
2. WHERE: Using this clause is used to filter records based on the condition.
Syntax:
Select column1, column2,...column N
From table_name
Where condition;
Example:
SELECT * FROM person WHERE department = 'HR';
3. INSERT INTO: This statement is used to add new data in the database.
Syntax:
INSERT INTO table_name (column1, column2, ...,column N)
VALUES (value1, value2, ..., value N);
Example:
INSERT INTO employees (person_name, address)
VALUES (‘XYZ’,’Noida’);
Syntax: (Insert data into all columns)
INSERT INTO table_name
VALUES (value1, value2, value);
Example:
INSERT INTO employees
VALUES (‘Alice’, 30, 'HR', 550);
4. UPDATE: This command is utilized to update existing values in a record in the table.
Syntax:
UPDATE table_name
SET column1 = value1,
column2 = value2
WHERE condition;
Example:
UPDATE person
SET salary = 550
WHERE name = “Alice'';
Explanation:
This increases Alice salary to 550.
5. DELETE: This statement deletes the existing data from the table based on the given condition.
Syntax:
DELETE FROM table_name
WHERE condition;
Example:
DELETE FROM employees
WHERE name = 'John Doe';
Explanation:
This will delete John Doe's record from the table.
6. ORDER BY: In SQL, this command is used to manage the data in Descending order (DESC) or Ascending order (ASC). By default, this keyword is used to set the result in ASC order.
Syntax:
SELECT column1, column2,...column N
FROM table_name
ORDER BY column1, column2,...column N ASC|DESC;
Example:
SELECT * FROM employees
ORDER BY salary DESC;
Explanation:
Display employees in descending order of their salary.
7. LIMIT: LIMIT is used to restrict the number of records being returned in the CTE (Common Table Expression).
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
Example:
SELECT * FROM employees
LIMIT 5;
Explanation:
Only the first 5 rows from the employee’s table will be returned to this query.
Note: Some databases like SQL servers use TOP or FETCH instead of LIMIT.
8. LIKE: SQL LIKE operator is used to retrieve the data from the table, based on the specific pattern. This operator contains the two types of the Wildcard Characters that are shown below:
% (Percent)- This wildcard character represents zero, one or more than one character.
_ (underscore)- This wildcard character represents only a single character.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE column N LIKE pattern;
Example:
SELECT * FROM employees
WHERE name LIKE 'J%';
Explanation:
This gives back all employees whose names start with the letter J.
9. COUNT: In SQL, the COUNT () function retrieve the total number of rows that meet the condition in a table.
Syntax:
SELECT COUNT (column_name)
FROM table_name
WHERE condition;
Example:
SELECT COUNT (*) FROM employees;
Explanation:
This query gives the total number of employees in the table.
SELECT count (*) FROM employees WHERE department = 'IT';
Explanation:
This query counts the employee who work in the IT department.
10. GROUP BY: In SQL, this clause is used to collect the data sets into the result based on the given condition. This clause often works with aggregate functions like (Count (), SUM (), AVG (), MIN () and MAX ()) to perform calculation on the data.
Syntax:
SELECT column1, aggregate_function (column 2)
From table_name
GROUP BY column1, column 2;
Example:
SELECT department, sum (*)
FROM employees
GROUP BY department;
Bonus: JOIN - Combine Data from Multiple Tables
Although beyond the absolute beginner level, JOIN is a critical concept for beginners.
Syntax:
SELECT column1, column2…, column N
FROM table1
JOIN table2
ON table1.column = table2.column;
Example:
SELECT employees.name, departments.name
FROM employees
JOIN departments ON employees.department_id = departments.id;
The query goes ahead to assess all two tables of information.
Conclusion:
Mastering the 10 basic SQL queries is essential for anyone starting out with databases. These foundational commands — including SELECT, WHERE, INSERT, UPDATE, DELETE, ORDER BY, GROUP BY, JOIN, LIKE, and LIMIT — provide the tools needed to retrieve, modify, and manage data effectively.
I suggest you learn SQL Programming language from the Tpoint tech website as this website provides SQL tutorials, interview questions, and an Online Compiler as well.