0% found this document useful (0 votes)
19 views

SQL 1

SQL is a language used to manage and retrieve data from databases. It allows users to perform tasks like querying and manipulating data, by allowing them to select, insert, update, and delete records in tables. Some important SQL commands include SELECT, INSERT, UPDATE, DELETE, CREATE DATABASE, and ALTER TABLE. SQL also uses operators like WHERE, ORDER BY, BETWEEN and JOIN to filter records and combine data from multiple tables.

Uploaded by

Shubham Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

SQL 1

SQL is a language used to manage and retrieve data from databases. It allows users to perform tasks like querying and manipulating data, by allowing them to select, insert, update, and delete records in tables. Some important SQL commands include SELECT, INSERT, UPDATE, DELETE, CREATE DATABASE, and ALTER TABLE. SQL also uses operators like WHERE, ORDER BY, BETWEEN and JOIN to filter records and combine data from multiple tables.

Uploaded by

Shubham Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

SQL

SUBSCRIBE FOR BPSC TRE Computer Science Preparation


Created by: https://www.youtube.com/@jobsinbihar6360

 SQL stands for Structured Query Language

 What SQL can do?


a. SQL can execute queries against a database
b. SQL can retrieve data from a database
c. SQL can insert records in a database
d. SQL can update records in a database
e. SQL can delete records from a database

 Some of The Most Important SQL Commands


a. SELECT - extracts data from a database
b. UPDATE - updates data in a database
c. DELETE - deletes data from a database
d. INSERT INTO - inserts new data into a database
e. CREATE DATABASE - creates a new database
f. ALTER DATABASE - modifies a database
g. CREATE TABLE - creates a new table
h. ALTER TABLE - modifies a table
i. DROP TABLE - deletes a table
j. CREATE INDEX - creates an index (search key)
k. DROP INDEX - deletes an index

An index is a copy of selected columns of data, from a table, that is designed to


enable very efficient search.
SELECT Statement

The SELECT statement is used to select data from a database.

SELECT * FROM table_name;

To Select Entire Data of Table

SELECT * FROM table_name

Where Statement in SQL

SELECT column1, column2, ...

FROM table_name

WHERE condition;

SELECT * FROM Customers

WHERE CustomerID=1;

AND, OR and NOT Operators

AND Statement
SELECT * FROM Customers

WHERE Country='Germany' AND City='Berlin';

OR Statement

SELECT * FROM Customers

WHERE City='Berlin' OR City='München';

NOT Statement

SELECT * FROM Customers

WHERE NOT Country='Germany';

The following SQL statement selects all fields from "Customers" where country is
NOT "Germany":

SQL ORDER BY

The ORDER BY keyword sorts the records in ascending order by default. To sort
the records in descending order, use the DESC keyword.

SELECT * FROM Customers

ORDER BY Country;
Descending Order

SELECT * FROM Customers

ORDER BY Country DESC;

SQL INSERT INTO Statement

Specify both the column names and the values to be inserted:

INSERT INTO Customers (CustomerName, ContactName, Address, City,


PostalCode, Country)

VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006',


'Norway');

NULL Value

A field with a NULL value is a field with no value.

The term NULL in SQL is used to specify that a data value does not exist in the
database. It is not the same as an empty string or a value of zero, and it signifies
the absence of a value or the unknown value of a data field.

Some common reasons why a value may be NULL:

a. The value may not be provided during the data entry.


b. The value is not yet known.

It is important to understand that you cannot use comparison operators such as “=”,
“<”, or “>” with NULL values. This is because the NULL values are unknown and
could represent any value.

SELECT CustomerName, ContactName, Address


FROM Customers

WHERE Address IS NULL;

SQL UPDATE Statement

The UPDATE statement is used to modify the existing records in a table.

UPDATE Customers

SET Contact Name='John'

WHERE Country='Mexico';

SQL DELETE Statement

DELETE FROM Customers WHERE Customer Name='Atul Prasad';

SQL TOP & FETCH

SELECT * FROM Customers

WHERE Country='Germany'

FETCH FIRST 3 ROWS ONLY;


SELECT TOP 2 country

FROM table_name

SQL MIN() and MAX() Functions

The MIN() function returns the smallest value of the selected column.

The MAX() function returns the largest value of the selected column.

SELECT MAX(column_name)

FROM table_name

WHERE condition;

COUNT() Syntax

The COUNT() function returns the number of rows that matches a specified
criterion

SELECT COUNT(column_name)

FROM table_name

WHERE condition;

AVG() Syntax
The AVG() function returns the average value of a numeric column.

SELECT AVG(column_name)

FROM table_name

WHERE condition;

SUM() Syntax

The SUM() function returns the total sum of a numeric column.

SELECT SUM(column_name)

FROM table_name

WHERE condition;

SQL IN Operator

The IN operator allows you to specify multiple values in a WHERE clause.

SELECT * FROM Customers

WHERE Country IN ('Germany', 'France', 'UK');


SQL BETWEEN Operator

The BETWEEN operator selects values within a given range.

SELECT * FROM Products

WHERE Price BETWEEN 10 AND 20;

SELECT * FROM Products

WHERE Price NOT BETWEEN 10 AND 20;

SQL Aliases

SQL aliases are used to give a table, or a column in a table, a temporary name.
Aliases are often used to make column names more readable.

An alias is created with the AS keyword.

SELECT CustomerID AS ID, CustomerName AS Customer

FROM Customers;

SQL JOIN
A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.

Different Types of SQL JOINs

Here are the different types of the JOINs in SQL:

(INNER) JOIN: Returns records that have matching values in both tables

LEFT (OUTER) JOIN: Returns all records from the left table, and the matched
records from the right table

RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched
records from the left table

FULL (OUTER) JOIN: Returns all records when there is a match in either left or
right table

SQL INNER JOIN

The INNER JOIN keyword selects records that have matching values in both
tables.

SELECT column_name(s)

FROM table1

INNER JOIN table2

ON table1.column_name = table2.column_name;
SQL LEFT JOIN

The LEFT JOIN keyword returns all records from the left table (table1), and the
matching records from the right table (table2). The result is 0 records from the
right side, if there is no match.

SELECT column_name(s)

FROM table1

LEFT JOIN table2

ON table1.column_name = table2.column_name;

SQL RIGHT JOIN

SELECT column_name(s)

FROM table1

RIGHT JOIN table2

ON table1.column_name = table2.column_name;

SQL UNION Operator

The UNION operator is used to combine the result-set of two or more SELECT
statements.
1. Every SELECT statement within UNION must have the same number of
columns.
2. The columns must also have similar data types

SELECT column_name(s) FROM table1

UNION

SELECT column_name(s) FROM table2;

SQL SELECT INTO Statement

The SELECT INTO statement copies data from one table into a new table.

SELECT CustomerName, ContactName INTO CustomersBackup2017

FROM Customers;

SUBSCRIBE FOR BPSC TRE Computer Science Preparation


Created by: https://www.youtube.com/@jobsinbihar6360

You might also like