Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

SQL Crashcourse

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

SQL Tutorial for Beginners | Full SQLntroduction to

SQL
In today's world, data is everything, but to manage it, one has to master
the art of data management. To play with data and database, we need
to know the language that is understood by these databases, and that
language is SQL. SQL is a core of relational type databases. Because of
its huge popularity, it is used by most companies worldwide.

What is SQL?
SQL is an abbreviation for structured query language. It is a language
used to perform actions such as update, retrieve, manipulate, and store
data on a relational database.

Interesting Facts about SQL

 SQL was developed at IBM by Donald D. Chamberlain and


Raymond F. Boyce in the early 1970s.
 SQL is said to be a powerful language because it uses very
simple English sentences and also uses very few lines.
 SQL is known to be a declarative language, which means that
when you write a query, you have to describe what needs to be
done, and you don't have to worry about the flow of the query.
Features of SQL

 SQL has well-defined standards.


 SQL is easy to learn.
 In SQL, we can create multiple views.
 SQL queries are portable in nature.

Data and Database


What is Data?
Data is values from differences which have been translated for some
purpose, and this purpose can be anything. The source for the data can
be like temperature reading, financial data, videos, blogs, text, etc. It
should make a meaningful sense, only then we can consider it as data.
What is Database?
A database is an organized collection of data where the data is stored
and accessed electronically from a computer system. It is like a library,
and the books are the data.

Types of Databases
There are different kinds of databases that are in use today, and these
databases are classified based on capabilities, features, functionality,
size, and performance. Few of them are:

 Distributed database
 Object-oriented database
 Centralized database
 Operational database
 Graph database
 Cloud database
 NoSQL database
 Relational database
The two popular types that are widely used are the relational database
and NoSQL database.

Popular Databases
Some of the popular databases that are widely used today are
MongoDB, Postgres, Microsoft Access, Microsoft SQL Server, MySQL,
and Oracle DB.

Queries Related to Database


Basic database related queries:

 How to create a database?


 How to delete a database?
 How to create a table?
What is a Table?
A table in a database is a collection of data in a tabular way. It consists
of rows and columns. The table contains data elements, also known as
values, using a model of vertical columns and horizontal rows. The point
of intersection of a row and a column is called a cell.

In the world of databases, a table is a collection of data stored in rows


and columns. Each row in a table represents a single record, while each
column represents a specific attribute of that record. In this article, we
will explore the basics of creating and manipulating tables in SQL.

The Basics of Tables in SQL


A table consists of tuples, which are single rows of data representing a
single record in the table. Each tuple has attributes, which are the
specific features of an entity. Attributes have a name and a data type.
There are several types of constraints that can be used to ensure data
integrity within a table, including  check ,  default ,  primary
key ,  foreign key ,  not null ,  index , and  unique .

SQL Queries for Tables


The most basic SQL query for tables is the  SELECT  statement, which is
used to retrieve data from a table. The  WHERE  clause is used to filter
records based on a specified condition. Operators such as  AND ,  OR ,
and  NOT  can be used to add complexity to the query.

Another important SQL query for tables is the  INSERT


INTO  statement, which is used to insert new records into a table.
Aggregate functions such as  COUNT ,  SUM ,  AVERAGE ,  MINIMUM ,
and  MAXIMUM  can be used to group and manipulate data in various
ways.

Example SQL Queries for Tables

 Creating a Table: To create a table, use the  CREATE


TABLE  statement, followed by the table name and columns
with their respective data types. End the statement with a
semicolon. Example:  CREATE TABLE emp (id INT,
first_name VARCHAR(50), last_name VARCHAR(50),
date_of_birth DATE);

 Deleting a Table: To delete a table, use the  DROP


TABLE  statement, followed by the table name.
Example:  DROP TABLE emp;

 Retrieving All Columns from a Table: To retrieve all columns


from a table, use the  SELECT *  statement. Example:  SELECT
* FROM student;

 Retrieving Specific Columns from a Table: To retrieve specific


columns from a table, use the  SELECT  statement followed by
the column names. Example:  SELECT first_name,
last_name FROM student;

 Filtering Records with WHERE Clause: To filter records based


on a condition, use the  WHERE  clause. Example:  SELECT
first_name FROM student WHERE city = 'Goa';

 Using AND Operator: To add multiple conditions to


the  WHERE  clause, use the  AND  operator. Example:  SELECT
* FROM student WHERE first_name = 'Bharat' AND
last_name = 'Singh';

 Using OR Operator: To add multiple conditions to


the  WHERE  clause, use the  OR  operator. Example:  SELECT *
FROM student WHERE first_name = 'Bhuvi' OR
last_name = 'Kumar';

 Using NOT Operator: To negate a condition in


the  WHERE  clause, use the  NOT  operator. Example:  SELECT
* FROM student WHERE first_name != 'Ashok';
 Inserting Records into a Table: To insert new records into a
table, use the  INSERT INTO  statement, followed by the
column names and their values. Example:  INSERT INTO
student (first_name, last_name, address, city,
marks) VALUES ('Manoj', 'Sharma', '07 MG
Road', 'Jaipur', 438);

 Using COUNT Function: To count the number of records that


match a condition, use the  COUNT  function.
Example:  SELECT

SQL Aggregate Functions


Aggregate functions are used to perform calculations on a set of values
and return a single value. The three aggregate functions in SQL are:

 SUM: used to find the total sum of a column


 MIN: used to find the smallest value in a column
 MAX: used to find the largest value in a column
To use aggregate functions in SQL, the syntax is:

SELECT function(column_name) FROM table_name WHERE


condition;

For example, to find the total marks scored by all the students:

SELECT SUM(marks) FROM student;

Similarly, to find the student name who has scored minimum marks:

SELECT MIN(name) FROM student WHERE marks=(SELECT


MIN(marks) FROM student);

And to find the student who has scored maximum marks:


SELECT MAX(name) FROM student WHERE marks=(SELECT
MAX(marks) FROM student);

SQL Group By and Having Clauses


The GROUP BY clause is used to group data with similar values together.
The syntax is:

SELECT column_name FROM table_name GROUP BY


column_name;

The HAVING clause is used to place conditions on the groups created


by the GROUP BY clause. The syntax is:

SELECT column_name FROM table_name GROUP BY


column_name HAVING condition;

SQL Null Values


Null values in SQL represent missing or unknown data. To check for null
values, use the IS NULL or IS NOT NULL operators. The syntax is:

SELECT column_name FROM table_name WHERE column_name


IS NULL;

SELECT column_name FROM table_name WHERE column_name


IS NOT NULL;

SQL Update and Delete Commands


The UPDATE command is used to modify rows in a table, and the
DELETE command is used to delete rows from a table. The syntax is:

UPDATE table_name SET column_name=value WHERE


condition;

DELETE FROM table_name WHERE condition;


SQL In and Between Operators
The IN operator is used to specify multiple values inside the WHERE
clause, and the BETWEEN operator selects a particular value within a
specified range. The syntax is:

SELECT column_name FROM table_name WHERE column_name


IN (value1, value2, ...);

SELECT column_name FROM table_name WHERE column_name


BETWEEN value1 AND value2;

SQL Aliases
Aliases are used to give temporary names to a column or table. The
syntax for column aliasing is:

SELECT column_name AS alias_name FROM table_name;

The syntax for table aliasing is:

SELECT column_name FROM table_name AS alias_name;

When working with SQL, it's common to use table aliasing as a


technique. By using this method, you can display the names of students
from a student table, as shown in the example query. This is a basic
concept of SQL, and we hope you found this session informative and
interesting. Remember that SQL is used to manage data, databases,
tables, and queries.

If you have any doubts or queries, feel free to comment, and we'll reply
as soon as possible. Don't forget to like this video and subscribe to our
channel for more content on SQL and other topics. Happy learning!

Course
Rishabh Mishra
SQL is a widely used programming language in various industries. This
course is designed for beginners who want to learn SQL from scratch. It
covers the basics and advanced concepts of SQL, including:

 Introduction to SQL
 Uses of SQL
SQL Application: SQL vs Excel
In the world of data management and analysis, two popular tools are
SQL and Excel. While both can be used to store and manipulate data,
they have different strengths and weaknesses.

 Structure: SQL is designed for relational databases, which


means that data is stored in tables that are connected through
relationships. Excel, on the other hand, is more flexible and can
be used for a variety of data structures.
 Scale: SQL is better suited for large datasets and complex
queries, while Excel may struggle with large amounts of data
and can be slower to process complex calculations.
 Functionality: SQL is better for data manipulation, aggregation,
and filtering, while Excel is better for visualizing data and
creating charts and graphs.
Overall, the choice between SQL and Excel depends on the specific
needs of the user and the type of data being analyzed. While Excel may
be more user-friendly for basic data tasks, SQL offers greater scalability
and functionality for more complex data management and analysis.

ntroduction to SQL
In today's world, data is everything, but to manage it, one has to master
the art of data management. To play with data and database, we need
to know the language that is understood by these databases, and that
language is SQL. SQL is a core of relational type databases. Because of
its huge popularity, it is used by most companies worldwide.

What is SQL?
SQL is an abbreviation for structured query language. It is a language
used to perform actions such as update, retrieve, manipulate, and store
data on a relational database.

Interesting Facts about SQL

 SQL was developed at IBM by Donald D. Chamberlain and


Raymond F. Boyce in the early 1970s.
 SQL is said to be a powerful language because it uses very
simple English sentences and also uses very few lines.
 SQL is known to be a declarative language, which means that
when you write a query, you have to describe what needs to be
done, and you don't have to worry about the flow of the query.
Features of SQL

 SQL has well-defined standards.


 SQL is easy to learn.
 In SQL, we can create multiple views.
 SQL queries are portable in nature.

Data and Database


What is Data?
Data is values from differences which have been translated for some
purpose, and this purpose can be anything. The source for the data can
be like temperature reading, financial data, videos, blogs, text, etc. It
should make a meaningful sense, only then we can consider it as data.

What is Database?
A database is an organized collection of data where the data is stored
and accessed electronically from a computer system. It is like a library,
and the books are the data.

Types of Databases
There are different kinds of databases that are in use today, and these
databases are classified based on capabilities, features, functionality,
size, and performance. Few of them are:
 Distributed database
 Object-oriented database
 Centralized database
 Operational database
 Graph database
 Cloud database
 NoSQL database
 Relational database
The two popular types that are widely used are the relational database
and NoSQL database.

Popular Databases
Some of the popular databases that are widely used today are
MongoDB, Postgres, Microsoft Access, Microsoft SQL Server, MySQL,
and Oracle DB.

Queries Related to Database


Basic database related queries:

 How to create a database?


 How to delete a database?
 How to create a table?
What is a Table?
A table in a database is a collection of data in a tabular way. It consists
of rows and columns. The table contains data elements, also known as
values, using a model of vertical columns and horizontal rows. The point
of intersection of a row and a column is called a cell.

In the world of databases, a table is a collection of data stored in rows


and columns. Each row in a table represents a single record, while each
column represents a specific attribute of that record. In this article, we
will explore the basics of creating and manipulating tables in SQL.

The Basics of Tables in SQL


A table consists of tuples, which are single rows of data representing a
single record in the table. Each tuple has attributes, which are the
specific features of an entity. Attributes have a name and a data type.
There are several types of constraints that can be used to ensure data
integrity within a table, including  check ,  default ,  primary
key ,  foreign key ,  not null ,  index , and  unique .

SQL Queries for Tables


The most basic SQL query for tables is the  SELECT  statement, which is
used to retrieve data from a table. The  WHERE  clause is used to filter
records based on a specified condition. Operators such as  AND ,  OR ,
and  NOT  can be used to add complexity to the query.

Another important SQL query for tables is the  INSERT


INTO  statement, which is used to insert new records into a table.
Aggregate functions such as  COUNT ,  SUM ,  AVERAGE ,  MINIMUM ,
and  MAXIMUM  can be used to group and manipulate data in various
ways.

Example SQL Queries for Tables

 Creating a Table: To create a table, use the  CREATE


TABLE  statement, followed by the table name and columns
with their respective data types. End the statement with a
semicolon. Example:  CREATE TABLE emp (id INT,
first_name VARCHAR(50), last_name VARCHAR(50),
date_of_birth DATE);

 Deleting a Table: To delete a table, use the  DROP


TABLE  statement, followed by the table name.
Example:  DROP TABLE emp;
 Retrieving All Columns from a Table: To retrieve all columns
from a table, use the  SELECT *  statement. Example:  SELECT
* FROM student;

 Retrieving Specific Columns from a Table: To retrieve specific


columns from a table, use the  SELECT  statement followed by
the column names. Example:  SELECT first_name,
last_name FROM student;

 Filtering Records with WHERE Clause: To filter records based


on a condition, use the  WHERE  clause. Example:  SELECT
first_name FROM student WHERE city = 'Goa';

 Using AND Operator: To add multiple conditions to


the  WHERE  clause, use the  AND  operator. Example:  SELECT
* FROM student WHERE first_name = 'Bharat' AND
last_name = 'Singh';

 Using OR Operator: To add multiple conditions to


the  WHERE  clause, use the  OR  operator. Example:  SELECT *
FROM student WHERE first_name = 'Bhuvi' OR
last_name = 'Kumar';

 Using NOT Operator: To negate a condition in


the  WHERE  clause, use the  NOT  operator. Example:  SELECT
* FROM student WHERE first_name != 'Ashok';

 Inserting Records into a Table: To insert new records into a


table, use the  INSERT INTO  statement, followed by the
column names and their values. Example:  INSERT INTO
student (first_name, last_name, address, city,
marks) VALUES ('Manoj', 'Sharma', '07 MG
Road', 'Jaipur', 438);
 Using COUNT Function: To count the number of records that
match a condition, use the  COUNT  function.
Example:  SELECT

SQL Aggregate Functions


Aggregate functions are used to perform calculations on a set of values
and return a single value. The three aggregate functions in SQL are:

 SUM: used to find the total sum of a column


 MIN: used to find the smallest value in a column
 MAX: used to find the largest value in a column
To use aggregate functions in SQL, the syntax is:

SELECT function(column_name) FROM table_name WHERE


condition;

For example, to find the total marks scored by all the students:

SELECT SUM(marks) FROM student;

Similarly, to find the student name who has scored minimum marks:

SELECT MIN(name) FROM student WHERE marks=(SELECT


MIN(marks) FROM student);

And to find the student who has scored maximum marks:

SELECT MAX(name) FROM student WHERE marks=(SELECT


MAX(marks) FROM student);

SQL Group By and Having Clauses


The GROUP BY clause is used to group data with similar values together.
The syntax is:
SELECT column_name FROM table_name GROUP BY
column_name;

The HAVING clause is used to place conditions on the groups created


by the GROUP BY clause. The syntax is:

SELECT column_name FROM table_name GROUP BY


column_name HAVING condition;

SQL Null Values


Null values in SQL represent missing or unknown data. To check for null
values, use the IS NULL or IS NOT NULL operators. The syntax is:

SELECT column_name FROM table_name WHERE column_name


IS NULL;

SELECT column_name FROM table_name WHERE column_name


IS NOT NULL;

SQL Update and Delete Commands


The UPDATE command is used to modify rows in a table, and the
DELETE command is used to delete rows from a table. The syntax is:

UPDATE table_name SET column_name=value WHERE


condition;

DELETE FROM table_name WHERE condition;

SQL In and Between Operators


The IN operator is used to specify multiple values inside the WHERE
clause, and the BETWEEN operator selects a particular value within a
specified range. The syntax is:

SELECT column_name FROM table_name WHERE column_name


IN (value1, value2, ...);
SELECT column_name FROM table_name WHERE column_name
BETWEEN value1 AND value2;

SQL Aliases
Aliases are used to give temporary names to a column or table. The
syntax for column aliasing is:

SELECT column_name AS alias_name FROM table_name;

The syntax for table aliasing is:

SELECT column_name FROM table_name AS alias_name;

When working with SQL, it's common to use table aliasing as a


technique. By using this method, you can display the names of students
from a student table, as shown in the example query. This is a basic
concept of SQL, and we hope you found this session informative and
interesting. Remember that SQL is used to manage data, databases,
tables, and queries.

If you have any doubts or queries, feel free to comment, and we'll reply
as soon as possible. Don't forget to like this video and subscribe to our
channel for more content on SQL and other topics. Happy learning!

You might also like