SQL statements
SQL statements instruct the Database about the operation you want to do on the structured data and the information you want to get from it.
SQL statements are straightforward to use and comprehend. They're similar to plain English but with a specific syntax.
Example
SELECT col1, col2 FROM tableName;
Each SQL statement starts with a SQL term and concludes with a semicolon( ";" ). In SQL, the semicolon is used to separate numerous SQL statements that will be executed in the same call.
Also see, SQL EXCEPT
Some of The Most Important SQL Commands
SELECT Statement
This SQL statement retrieves information from a SQL database and displays it as output to the database user.
SELECT col1, col2....colN
FROM tableName;
WHERE Clause
The records are filtered using this SQL query. Only queries that satisfy the following CONDITIONs are returned.
SELECT col1, col2....colN
FROM tableName
WHERE CONDITION;
IN Clause
The "IN" operator reduces the need for several "OR" conditions in a SQL query.
SELECT col1, col2....colN
FROM tableName
WHERE columnName IN (val1, val2,...valN);
ORDER BY Clause
When we wish to arrange records based on fields stored in SQL database tables, we use the "ORDER BY" clause in SQL.
SELECT col1, col2....colN
FROM tableName
WHERE CONDITION
ORDER BY columnName {ASC|DESC};
GROUP BY Clause
The "Group By" statement in SQL is used to group similar records.
SELECT SUM(columnName)
FROM tableName
WHERE CONDITION
GROUP BY columnName;
HAVING Clause
The HAVING clause in the SELECT statement places the condition in the groups indicated by the GROUP BY clause.
SELECT SUM(columnName)
FROM tableName
WHERE CONDITION
GROUP BY columnName
HAVING (CONDITION);
CREATE DATABASE Statement
It creates a database named after the name supplied in the Create Database statement.
CREATE DATABASE databaseName;
CREATE TABLE Statement
To build a table in a database, use the “CREATE TABLE” statement.
CREATE TABLE tableName(
col1 datatype,
col2 datatype,
col3 datatype,
.....
colN datatype,
PRIMARY KEY( one or more columns )
);
ALTER TABLE Statement
ALTER TABLE tableName {ADD|DROP|MODIFY} columnName {datatype};
DROP TABLE Statement
A DROP TABLE statement deletes a table's definition and all of its contents.
DROP TABLE tableName;
INSERT INTO Statement
This SQL statement adds new data or records to an existing database table. This statement can quickly insert single and numerous records in a single query line.
INSERT INTO tableName( col1, col2....colN)
VALUES ( val1, val2....valN);
UPDATE Statement
The current records in the SQL database are changed or modified by this SQL statement.
UPDATE tableName
SET col1 = val1, col2 = val2....colN=valN
[ WHERE CONDITION ];
DELETE Statement
This statement deletes existing records from the table.
DELETE FROM tableName
[ WHERE CONDITION ];
Must Read SQL Clauses
FAQs
-
List some of the aggregate functions used in SQL?
Sum, count, min, max are some of the aggregate functions used in SQL.
-
List some of the data types in SQL?
Char(size), Varchar(length), Text, Integer are some of the data types in SQL.
-
What are the different types of case manipulation functions available in SQL?
Three different types of manipulation functions in SQL are:
- LOWER: returns the string in lowercase.
- UPPER: returns the string in uppercase.
- INITCAP: returns the string with the first letter as uppercase and the rest in lowercase letters.
-
What is a Primary Key?
The PRIMARY KEY constraint uniquely identifies each row in a table. It contains an implied NOT NULL condition and must include UNIQUE data.
Key Takeaways
Cheers if you reached here!! In this blog, we learned about some basic syntax of SQL.
Also Read - TCL Commands In SQL
Don't stop here, Ninja; check out the Top 100 SQL Problems to get hands-on experience with frequently asked interview questions and land your dream job.
Yet learning never stops, and there is a lot more to learn. Happy Learning!!