What are Basic SQL Statements?

Overview

SQL is a very powerful language that consists of various statements to perform specific operations on the data. In this article, we will explain some of the fundamental SQL statements that are being used in the database management system.

What are SQL Statements?

SQL statements are basically a block, the foundation of the database management system. These statements allow us to interact with the database i.e. retrieve stored data, modify data, and perform various operations. SQL has a rich set of commands, but keeping simplicity in mind, we will focus on some of the most essential ones.

SQL Statements - SQL-min

Types of SQL Statements

There are 15 types of SQL Statements that are called the bread and butter of SQL statements as follows –

  1. Select Statement
  2. From Clause
  3. Where Clause
  4. Order By Clause
  5. Group By Clause
  6. Having Clause
  7. Join Statement
  8. Union Statement
  9. Subqueries
  10. Insert Statement
  11. Update Statement
  12. SET Clause
  13. Delete Statement
  14. Drop Statement
  15. Truncate Statement

Select Statement

The select statement is the backbone of SQL. It retrieves data from one or more database tables. For example, to retrieve all the data from a table named “Wildcard_Course” you would use as following example –

Syntax:

SELECT * FROM Table_Name

Example:

SELECT * FROM Wildcard_Course
01. SELECT-min

From Clause

The from clause specifies the source table name from which data should be retrieved. In the previous example, “Wildcard_Course” is the source table.

Where Clause: The where clause is used to filter data based on specified conditions. For example, if you want to retrieve only the course pertaining to SQL, you would write as following example –

Syntax:

SELECT * FROM Table_Name WHERE Condition

Example:

SELECT * FROM Wildcard_Course WHERE Course_Name LIKE '%SQL%'
2. WHERE-min

Order By Clause

Order by clause is used to sort the result set in ascending or descending order based on one or more columns. For ascending order shortcode is used as “ASC” and for descending order shortcode is used as “DESC”. SQL is used in ascending order by default. Refer below examples –

Syntax:

SELECT * FROM Table_Name WHERE Condition ORDER BY ASC/DESC

Example:

SELECT * FROM Wildcard_Course WHERE Course_Name LIKE '%SQL%' ORDER BY Course_Name
SELECT * FROM Wildcard_Course WHERE Course_Name LIKE '%SQL%' ORDER BY Course_Name ASC
SELECT * FROM Wildcard_Course WHERE Course_Name LIKE '%SQL%' ORDER BY Course_Name DESC
03. ORDER BY-min

Group By Clause

Group by clause is used to a grouping of rows with similar values into summary rows. It is often used with aggregate functions like SUM, AVG, and COUNT to analyze data.

Syntax:

SELECT Column_Name, COUNT(Column_Name) AS [Alias Name] FROM Table_Name GROUP BY Column_Name;

Example:

SELECT Department, COUNT(EmployeeName) AS [No Of Employee] FROM Employee GROUP BY Department;
04. GROUP BY-min

Having Clause

The having clause filters the results of a group by query based on specified conditions. It is used to filter aggregated data as follows –

Syntax:

SELECT Column_Name, COUNT(Column_Name) AS [Alias Name] FROM Table_Name GROUP BY Column_Name HAVING Condition;

Example:

SELECT Department, COUNT(EmployeeName) AS [No Of Employee] FROM Employee GROUP BY Department HAVING COUNT(EmployeeName)>2;
05. HAVING CLAUSE-min

Join Statement

The join statement is used to combine the result sets of two or more SELECT statements into a single result set as follows –

Example:

SELECT e.,ne.
FROM Employee e
JOIN NN_Employees3 ne ON ne.EmployeeID=e.EmployeeID
06. JOIN-min

Union Statement

SQL allows you to combine data from multiple tables using JOIN statements. You can perform inner, outer, or right joins to retrieve data from related tables as follows –

Example:

SELECT EmployeeID, EmployeeName FROM Employee
UNION
SELECT EmployeeID, CONCAT(FirstName,' ', LastName) AS [EmployeeName] FROM NN_Employees3
07. UNION-min

Subqueries

Subqueries are nested queries within a main query. They are used to retrieve data based on the result of another query.

Example:

SELECT * FROM Employee WHERE EmployeeID IN (SELECT EmployeeID FROM NN_Employees3)
08. SUBQUERY-min

Insert Statement

The INSERT INTO statement is used to add new records to a table. To insert a new employee into the “Employee” table as follows –

Example:

INSERT INTO Employee (EmployeeID,EmployeeName,Designation,Department,Salary)
VALUES(20,'Vaishnavi','AM','Call Center',25000)
INSERT INTO Employee (EmployeeID,EmployeeName,Designation,Department,Salary)
VALUES(21,'Trisha','DM','Call Center',45000)
09. INSERT Statements-min

Update Statement

The UPDATE statement allows you to modify existing records in a table. For example, to update all employee’s salaries by increasing 5000 of the call center department as follows –

Example:

UPDATE Employee SET EmployeeName='Akram Ali' WHERE EmployeeID=13 AND EmployeeName='Ali'
UPDATE Employee SET EmployeeName=CONCAT(EmployeeName,' ','Thhappa') WHERE Department='Call Center';
UPDATE Employee SET Salary=Salary+5000 WHERE Department='Call Center';
10 UPDATE Statements-min

Set Clause

A set clause is used to specify the column names to provide the desired value to override refer to the examples in the previous example.

Delete Statement

The DELETE statement is used to remove records from a table. To delete an employee from the “Employee” table

Example:

DELETE FROM Employee WHERE EmployeeID=22 AND EmployeeName='Trishul Twinkle'
11. DELETE-min

Drop Statement

A drop table statement is used to delete or remove the table permanently from the database as follows –

Example:

DROP TABLE NN_Employees3
12. DROP Table Statements-min

Truncate Statement

Truncate table statement is used to delete or remove records from a table and also resets the identity counter permanently as follows –

Example:

TRUNCATE TABLE NN_Employees3
13. TRUNCATE Table Statements-min

Best Practices for Writing SQL Statements

Refer to the below best practices for writing SQL statements –

  1. Use Proper Indentation: Organize your SQL statements for readability, especially when dealing with complex queries.
  2. Comment Your Code: Comments provide context and add to understanding the purpose of your SQL statements.
  3. Avoid Using *(Asterisk): It is advisable to specify the exact columns you need rather than selecting all columns with SELECT *.
  4. Use Joins: When working with multiple tables, employ JOIN statements to combine data effectively.
  5. Test Your Statements: Always run your SQL statements in a testing environment before applying them to production data.

FAQs

What is the purpose of SQL statements?

SQL statements are used to interact with databases, allowing users to retrieve, manipulate, and manage data.

Why is the SELECT statement important in SQL?

The SELECT statement retrieves data from a database, allowing users to interact with and analyze their data.

How can I update records in an SQL database?

You can use the UPDATE statement to modify existing records in a table by specifying the column and new values.

What is the purpose of the GROUP BY clause in SQL?

The GROUP BY clause is used to group rows with similar values into summary rows, often used with aggregate functions.

How do I improve SQL query performance?

Indexing columns, optimizing queries, and using proper database design can enhance SQL query performance.

What are the common types of SQL statements?

The common types of SQL statements include SELECT, INSERT, UPDATE, DELETE, and various advanced statements like JOIN, GROUP BY, HAVING, and ORDER BY.

Conclusion

Understanding basic SQL statements is essential for anyone dealing with databases. These fundamental statements form the building blocks for more complex queries and data manipulation. We hope you like this very well.

Please share this on your social media network so that it can be available to even more needy persons, which may benefit them. If you have any questions, feedback, or suggestions regarding this tutorial, please contact us. Do comment right in the comments section below. We will consider your valuable input and try to give you a response ASAP.

Recommended Article Posts

What are Data Types in SQL Server?What is a Wildcard Character in SQL Server?
What are operators in SQL?What is SQL Full Form and Its Key Feature?