Professional Documents
Culture Documents
SQL For Beginners
SQL For Beginners
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.
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.
CREATE A TABLE
CREATE TABLE table_name ( column_1 data_type, column_2 data_type, column_3
data_type );
ADDING A ROW
INSERT INTO celebs (id, name, age) VALUES (1, 'Justin Bieber', 21);
TO SHOW TABLE
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;
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
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;
CONDITIONAL SELECTION
SELECT * FROM movies WHERE imdb_rating > 8;
(= equals
!= not equals
> greater than
COMPARISON/LISTING OF DATA
SELECT * FROM movies WHERE name LIKE 'Se_en';
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)
SORTING
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.
SUM
SELECT SUM(downloads) FROM fake_apps;
MAX
SELECT MAX(downloads)FROM fake_apps;