Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

SQL

SQL is the worlds most popular language for managing and manipulating databases.
Why learn SQL? We live in a data-driven world now, and with all that data,
people have to search through it to find insights to help inform strategy,
marketing, operations, and a plethora of other categories. There are a ton
of business that use large, relational databases, which makes basic
understanding of SQL is a great employable skill not only for data
scientists, but for almost everyone.

SQL, 'Structured Query Language', is a programming language


designed to manage data stored in relational databases. SQL
operates through simple, declarative statements. This keeps data
accurate and secure, and helps maintain the integrity of databases,
regardless of size.

The SQL language is widely used today across web frameworks and
database applications. Knowing SQL gives you the freedom to
explore your data, and the power to make better decisions.

A statement is text that the database recognizes as a valid command.


Statements always end in a semi-colon ;

CREATE A TABLE
CREATE TABLE table_name ( column_1 data_type, column_2 data_type, column_3
data_type );

CREATE TABLE is a clause. Clauses perform specific tasks in SQL. By


convention, clauses are written in capital letters. Clauses can also be
referred to as commands.

table_name refers to the name of the table that the command is


applied to

(column_1 data_type, column_2 data_type, column_3 data_type) is


a parameter. A parameter is a list of columns, data types, or values
that are passed to a clause as an argument. Here, the parameter is
a list of column names and the associated data type.

ADDING A ROW
INSERT INTO celebs (id, name, age) VALUES (1, 'Justin Bieber', 21);

INSERT INTO Inserts a new row with 1, JB, 21

TO SHOW TABLE

SELECT * FROM celebs;

SELECT column 1 (SUM(g)/g), column2, column 3,


FROM xyz, condition (GROUP BY/ORDER BY);
..generalised formula

Will show the content of the table (* You can also query data from all
columns in a table SELECT name FROM celebs;)
SELECT statements are used to fetch data from a database.
Name (*) specifies the column to query data from
SELECT statements always return a new table called the

result set.

EDITING A ROW

edit a row

UPDATE celebs
SET age = 22
WHERE id = 1;

UPDATE is for editing the row content, SET is the desired


change to be made (along with the change in which
parameter), WHERE is the location of change to be made
(can be referred to by only referring to the first column ID
but change in other parametersthis is only to refer to the
particular row)
COLUMN ADDING

ALTER TABLE celebs ADD COLUMN twitter_handle TEXT;

The ALTER TABLE statement added a new column to the table. You
can use this command when you want to add columns to a table.
ADD COLUMN is a clause that lets you add a new column to a table.
TEXT is the data type for the new column

(The difference from ROW adding to COLUMN adding is that in ROW,


the parameters had to be mentioned in table_name () first, then the
required values must be added to VALUES ()but in COLUMN
adding, only the column Parameter name is to be entered as ADD
COLUMN xyz and the data type as TEXT)

DELETE ROWS
DELETE FROM celebs WHERE twitter_handle IS NULL;

The DELETE FROM statement deletes one or more rows from a table.
IS NULL is a condition in SQL that returns true when the value is
NULL and false otherwise.

RETRIEVAL OF DATA
SELECT will be used more often, coz it is the command to show the table
DISTINCT VALUES (selecting)
SELECT DISTINCT genre FROM movies;

SELECT DISTINCT is used to return unique values in the result set. It


filters out all duplicate values. Here, the result set lists each genre in
the movies table exactly once.

CONDITIONAL SELECTION
SELECT * FROM movies WHERE imdb_rating > 8;

WHERE is used to put a condition

(= equals
!= not equals
> greater than

< less than


>= greater than or equal to
<= less than or equal to)

COMPARISON/LISTING OF DATA
SELECT * FROM movies WHERE name LIKE 'Se_en';

LIKE can be a useful operator when you want to compare similar


values. Here, we are comparing two movies with the same name but
are spelled differently. The characters has to be inside .
LIKE is a special operator used with the WHERE clause to search for
a specific pattern in a column.
The _ means you can substitute any individual character here
without breaking the pattern. The names Seven and Se7en both
match this pattern.

SELECT * FROM movies WHERE name LIKE 'a%';

LIKE a% all the names starting with a


% any thing..if before a text % is used then any word before is
accepted, same with after
% is a wildcard character that matches zero or more missing letters
in the pattern.
%a all names ending with a

BETWEEN X AND Y
SELECT * FROM movies WHERE year BETWEEN '1990' AND '2000';

The BETWEEN operator is used to filter the result set within a certain
range. The values can be numbers, text or dates.

2 CONDITIONS (AND)
SELECT * FROM movies
WHERE year BETWEEN 1990 AND 2000
AND genre = 'comedy';
2 CONDITIONS (OR)

SELECT * FROM movies WHERE genre = 'comedy' OR year < 1980;

OR is an operator that filters the result set to only include rows


where either condition is true. Here, we return movies that either
have a genre of comedy or were released before 1980.

SORTING

SELECT * FROM movies ORDER BY imdb_rating DESC LIMIT 3;

ORDER BY Sorting the results often makes the data more useful and
easier to analyze.
DESC and ASC (low to high or A-Z)
LIMIT is a clause that lets you specify the maximum number of rows
the result set will have. Here, we specify that the result set can not
have less than three rows.

CALCULATIONS USING SQL


COUNT
SELECT COUNT (*) FROM fake_apps;

COUNT() is a function that takes the name of a column as an


argument and counts the number of rows where the column is not
NULL. Here, we want to count every row so we pass * as an
argument.

SELECT price, COUNT(*) FROM fake_apps GROUP BY price;

GROUP BY is a clause in SQL that is only used with aggregate


functions. It is used in collaboration with the SELECT statement to
arrange identical data into groups.

SUM
SELECT SUM(downloads) FROM fake_apps;

SUM() is a function that takes the name of a column as an argument


and returns the sum of all the values in that column. Here, it adds all
the values in the downloads column.

MAX
SELECT MAX(downloads)FROM fake_apps;

MAX() is a function that takes the name of a column as an argument and


returns the largest value in that column. Here, we pass downloads as an
argument so it will return the largest value in the downloads column.
ROUND OFF
SELECT price, ROUND(AVG(downloads), 2) FROM fake_apps GROUP BY
price;
ROUND OFF to 2 decimal points
ROUND(AVG(downloads))..ROUND OFF to nearest INTEGER

You might also like