For those of you who don’t know or are simply new to the term & concept of SQL, it stands for “Structured Query Language.” It’s a relatively simple language for coding, but it’s incredibly powerful. You can use SQL statements to get specific data from databases, update databases, and more.
SQL is used by many relational database management systems, such as MySQL, Oracle, and Microsoft SQL Server. Sybase, Ingres, and others also use SQL.
SQL is a highly sought-after language for data analysts. But it’s also useful for financial teams, programmers, non-technical people, and even marketers, sales reps, and other professionals who need to extract data from large databases. You don’t have to be an SQL all-star to use it, but knowing the basics will help you ask better questions (at least).
SQL is used to communicate with Relational Databases. Below are the following effects that SQL can do:
Note: Tables are the objects that store the data in Row and Column format.
The database is an organizational form to store the data in table format. Databases are further divided into two types:
Comment is used to write any note that will help other developers know the purpose of the code. The comment is only for reading purposes whenever we run the script, it will never execute.
SELECT * FROM Customers -- This is a comment |
---|
SELECT * FROM Customers # This is a comment |
---|
/* This is a multi-line comment. It can span multiple lines. */ SELECT * FROM Orders |
---|
The below figure shows the different types of commands that are used in SQL.
1) DDL: (Data Definition Language) used to create and manipulate the structure of the schema or database.
CREATE TABLE employees ( employee_id INT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), hire_date DATE ); |
---|
ALTER TABLE employees ADD email VARCHAR(100); ALTER TABLE employees MODIFY hire_date DATETIME; ALTER TABLE employees DROP COLUMN email; |
---|
DROP TABLE employees; |
---|
TRUNCATE TABLE table_name; |
---|
2) DML: (Data Manipulation Language) These commands are used to manipulate the data.
INSERT INTO table_name (column1, column2) VALUES (value1, value2); |
---|
UPDATE table_name SET column1 = new_value1, column2 = new_value2 WHERE condition; |
---|
Note: The ‘WHERE’ condition is a must while using the update command.
DELETE FROM table_name WHERE condition; |
---|
Note: WHERE condition is a must while using the DELETE command.
3) DQL: (Data Query Language) These commands are used to start the query.
SELECT column1, column2 FROM table_name WHERE condition; |
---|
4) DCL: (Data Control Language) These commands are used to grant and revoke the permissions to the person from the database.
GRANT privilege(s) ON object TO user_or_role; |
---|
REVOKE privilege(s) ON object FROM user_or_role; |
---|
5) TCL: (Transaction Control Language) These commands are used to deal with Transactions within the database.
COMMIT; |
---|
ROLLBACK; |
---|
SAVEPOINT savepoint_name; |
---|
In conclusion, the commands that are used in SQL are the building blocks to write the syntax and are very basic things to fetch the data from the database.
Keys are used to identify the Tuple/Record uniquely.
1) Candidate Key: Candidate Key is the column or group of columns that are used to identify the record uniquely.
2) Primary Key: The Primary Key is a column or group of columns that are used to identify the record uniquely. These are selected from the candidate Key.
CREATE TABLE students ( student_id INT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50) ); |
---|
3) Alternate Key: All columns in the candidate key are Alternate Key except the Primary Key.
4) Foreign Key: A Foreign Key is a column or set of columns that refer to the Primary Key of another table.
CREATE TABLE orders ( order_id INT PRIMARY KEY, customer_id INT, -- Foreign key referencing the customer table FOREIGN KEY (customer_id) REFERENCES customers (customer_id) ); |
---|
5) Unique Key: The unique Key is the same as the Primary Key, but it accepts only one null value.
CREATE TABLE products ( product_id INT UNIQUE, product_name VARCHAR(100) ); |
---|
In SQL, keys are the linchpins of database integrity and relationships. They ensure data uniqueness and enable efficient data retrieval, making them vital in database design. Mastering key concepts is essential for building robust and reliable database systems.
Constraints are a set of rules that are applied to the columns to maintain Data integrity.
CREATE TABLE students ( student_id INT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50) ); |
---|
CREATE TABLE orders ( order_id INT PRIMARY KEY, customer_id INT, -- Foreign key referencing the customer table FOREIGN KEY (customer_id) REFERENCES customers (customer_id) ); |
---|
CREATE TABLE products ( product_id INT UNIQUE, product_name VARCHAR(100) ); |
---|
CREATE TABLE employees ( employee_id INT, hire_date DATE, CHECK (hire_date >= '2000-01-01') ); |
---|
CREATE TABLE orders ( order_id INT PRIMARY KEY, order_date DATE DEFAULT GETDATE() ); |
---|
CREATE TABLE customers ( customer_id INT PRIMARY KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL ); |
---|
Constraints in SQL are the safeguards that ensure data delicacy and integrity within a database. They play a critical part in maintaining the trustability of data and administering business rules.
Clauses are the components of the SQL queries to manipulate, modify, and fetch the data from the Database.
1) Select: Select Clause is used to fetch the data from the database.
SELECT column1, column2 FROM table_name; |
---|
2) From: From Clause Specifies which table we use to retrieve the data.
SELECT column1, column2 FROM table_name; |
---|
3) Where: Where Clause is used to filter the data based on the condition.
SELECT column1, column2 FROM table_name WHERE condition; |
---|
4) Group By: Group By Clause is used to group the same data row-wise.
SELECT column1, SUM(column2) FROM table_name GROUP BY column1; |
---|
Note: Group by is used with Aggregate functions
5) Having: Having Clause is used to filter the data retrieved through grouping the data.
SELECT column1, SUM(column2) FROM table_name GROUP BY column1 HAVING SUM(column2) > 1000; |
---|
6) Order By: Order By Clause is used to sort the data in ascending or descending order.
SELECT column1, column2 FROM table_name ORDER BY column1 ASC; |
---|
Note: The order in ORDER BY is always ascending. (desc) is used for descending order
7) Limit/Offset: This clause is used to limit the no. of rows to fetch the data.
SELECT column1, column2 FROM table_name LIMIT 10 OFFSET 20; |
---|
SQL clauses are the essential elements that shape database queries and operations. They provide flexibility and control, allowing for precise data retrieval, filtering, and modification. Understanding SQL clauses is fundamental for harnessing the full potential of relational databases.
In conclusion, with the basics of SQL, you will be able to communicate with relational databases. It also provides you with a base to work with relational databases. This composition contains the introductory commands of SQL, which is a structure block to your SQL proficiency.
Through this composition, you will be able to get the knowledge to create, manipulate, insert, retrieve, update, and delete in the database. These skills are very precious for anyone who is involved in data management, data analytics, and more.