SQL Queries in a Nutshell- A beginner’s guide

0
66
SQL

SQL stands for structured query language. It is used to store and manipulate data in a database. It can store data in a database, retrieve the data, modify the data, insert or delete the data.

SQL database
SQL for databases

Concept of Database Tables

Every database contains one or more tables. Each table contains rows and columns. Each column represents an attribute and each row denotes a record of a person/place/thing. Every table in the database is provided with a unique name so that we can retrieve data from the table. Tables in a database can be linked to each other using keys such as primary keys, foreign keys, etc.

rows and columns in a database
Table structure

Some important SQL Commands

Select statement: used to display the data from the database.

SELECT * from table_name;
SELECT attr_1, attr_2 from table_name_2;
SELECT DISTINCT(attr_1) from table_name_3;

Here, * is used to consider all the attributes of the table, and table_name denotes any valid name of the table. In the second statement, we can specify the column names if we do not want to display entire columns of the database. In the third statement, the DISTINCT keyword is used which displays only unique values in the attr_1 column of table 3.

WHERE clause: displays value depending on the conditions specified in the statement.

SELECT * from table_name WHERE condition;

The WHERE clause displays value of only those records that satisfy the conditions specified in the WHERE clause. Basically it is used to filter records. Various operators such as =,<=,>= are used in the WHERE clause.

CREATE: This statement is used to create a table. This is the very first step, and later new entries are made in this table.

CREATE table table_name;

INSERT INTO : The INSERT INTO clause is used to insert values, records into the existing table.

INSERT into table_name (colmn_1, colmn_2,..) 
values (value1, value2,...);

colmn_1, colmn_2, … specifies the columns in which new data is to be inserted. Values are specified in the values(value1, value2,…)

UPDATE: We use this statement to modify the existing records in the table, based on conditions.

UPDATE table_name 
SET column_1=value, column_2=value,...
where condition;

SET statement sets new value of the attributes after the condition is satisfied.

DELETE statement: It either deletes record based on certain conditions, or delete the entire table.

DELETE from table_name WHERE conditions;

FAQ’s :

Name some aggregate functions of SQL.

Min, Max, Avg, Sum, Count are aggregate functions of SQL.

How do we represent single line comments in SQL?

— is used to represent single line comments in SQL

What is the use of LIKE operator used with WHERE clause?

LIKE operator is used to identify patterns in the string. _ represents single character while % represents multiple characters.