0% found this document useful (0 votes)
2 views19 pages

SQL Commands

The document provides an overview of materials and software needed to create a Smart Dustbin using Arduino, along with a comprehensive guide to SQL commands including SELECT, INSERT, UPDATE, DELETE, and various functions like COUNT, SUM, and AVG. It explains how to filter records using WHERE clauses, combine conditions with AND/OR, and perform JOIN operations between tables. Additionally, it covers the use of GROUP BY and HAVING clauses for aggregating data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views19 pages

SQL Commands

The document provides an overview of materials and software needed to create a Smart Dustbin using Arduino, along with a comprehensive guide to SQL commands including SELECT, INSERT, UPDATE, DELETE, and various functions like COUNT, SUM, and AVG. It explains how to filter records using WHERE clauses, combine conditions with AND/OR, and perform JOIN operations between tables. Additionally, it covers the use of GROUP BY and HAVING clauses for aggregating data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

https://www.cbsetuts.

com/important-questions-class-
12-computer-science-python-networking-open-
source-concepts/

https://youtu.be/zehwgTB0vV8
https://www.youtube.com/shorts/GlUzUuGvhEY?
feature=share
https://youtu.be/sZzszp7wOPE

Materials Required to make


Smart Dustbin: –
1. Arduino Uno

2. Ultrasonic sensor

3. Servo motor

4. Jumper wires

5. Battery

6. Dustbin

7. Cardboard

8. Adhesive

9. Scissor

Software used: – Arduino IDE


SQL COMMANDS:

 SELECT - extracts data from a database


 UPDATE - updates data in a database
 DELETE - deletes data from a database
 INSERT INTO - inserts new data into a database
 CREATE DATABASE - creates a new database
 ALTER DATABASE - modifies a database
 CREATE TABLE - creates a new table
 ALTER TABLE - modifies a table
 DROP TABLE - deletes a table
 CREATE INDEX - creates an index (search key)
 DROP INDEX - deletes an index

The SELECT statement is used to select data from


a database.
SELECT CustomerName, City FROM Customers;

Select ALL columns


If you want to return all columns, without specifying every
column name, you can use the SELECT * syntax:

SELECT * FROM Customers;

The SELECT DISTINCT statement is used to


return only distinct (different) values.
SELECT DISTINCT Country FROM Customers;
Count Distinct
By using the DISTINCT keyword in a function
called COUNT, we can return the number of different
countries.

SELECT COUNT(DISTINCT Country) FROM Customers;

The WHERE clause is used to filter records.


It is used to extract only those records that fulfill a
specified condition.

SELECT * FROM Customers


WHERE Country='Mexico';

Note: The WHERE clause is not only used


in SELECT statements, it is also used in UPDATE, DELETE,
etc.!

Operators in The WHERE


Clause
You can use other operators than the = operator to filter
the search.

SELECT * FROM Customers


WHERE CustomerID > 80;

The ORDER BY keyword is used to sort the result-set


in ascending or descending order.
Sort the products by price:
SELECT * FROM Products
ORDER BY Price;

Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

DESC
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 Products


ORDER BY Price DESC;

Order Alphabetically
For string values the ORDER BY keyword will order
alphabetically:

SELECT * FROM Products


ORDER BY ProductName;
Alphabetically DESC
To sort the table reverse alphabetically, use
the DESC keyword:

Example
Sort the products by ProductName in reverse order:
SELECT * FROM Products
ORDER BY ProductName DESC;

Using Both ASC and DESC


The following SQL statement selects all customers from
the "Customers" table, sorted ascending by the "Country"
and descending by the "CustomerName" column:

SELECT * FROM Customers


ORDER BY Country ASC, CustomerName DESC;

The AND operator is used to filter records based on


more than one condition, like if you want to return all
customers from Spain that starts with the letter 'G':
Select all customers from Spain that starts with the letter
'G':
SELECT *
FROM Customers
WHERE Country = 'Spain' AND CustomerName LIKE 'G
%';
All Conditions Must Be
True
The following SQL statement selects all fields
from Customers where Country is "Germany"
AND City is "Berlin" AND PostalCode is higher than
12000:

Example
SELECT * FROM Customers
WHERE Country = 'Germany'
AND City = 'Berlin'
AND PostalCode > 12000;

Combining AND and OR


You can combine the AND and OR operators.

The following SQL statement selects all customers from


Spain that starts with a "G" or an "R".

Make sure you use parenthesis to get the correct result.

Example
Select all Spanish customers that starts with either "G" or
"R":
SELECT * FROM Customers
WHERE Country = 'Spain' AND (CustomerName LIKE 'G
%' OR CustomerName LIKE 'R%');
The OR operator is used to filter records based on
more than one condition, like if you want to return all
customers from Germany but also those from Spain:

Select all customers from Germany or Spain:


SELECT *
FROM Customers
WHERE Country = 'Germany' OR Country = 'Spain';

At Least One Condition


Must Be True
The following SQL statement selects all fields from
Customers where either City is
"Berlin", CustomerName starts with the letter "G"
or Country is "Norway":

SELECT * FROM Customers


WHERE City = 'Berlin' OR CustomerName LIKE 'G
%' OR Country = 'Norway';

Combining AND and OR

Select all Spanish customers that starts with either "G" or


"R":
SELECT * FROM Customers
WHERE Country = 'Spain' AND (CustomerName LIKE 'G
%' OR CustomerName LIKE 'R%');

The SQL INSERT INTO


Statement
The INSERT INTO statement is used to insert new records
in a table.

INSERT INTO Syntax


It is possible to write the INSERT INTO statement in two
ways:

1. Specify both the column names and the values to be


inserted:
INSERT INTO table_name (column1, column2, column3
, ...)
VALUES (value1, value2, value3, ...);

INSERT INTO Customers (CustomerName, ContactName,


Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen
21', 'Stavanger', '4006', 'Norway');

Insert Data Only in


Specified Columns
INSERT INTO Customers (CustomerName, City,
Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');

Insert Multiple Rows


INSERT INTO Customers (CustomerName, ContactName,
Address, City, PostalCode, Country)
VALUES
('Cardinal', 'Tom B. Erichsen', 'Skagen
21', 'Stavanger', '4006', 'Norway'),
('Greasy Burger', 'Per Olsen', 'Gateveien
15', 'Sandnes', '4306', 'Norway'),
('Tasty Tee', 'Finn Egan', 'Streetroad
19B', 'Liverpool', 'L1 0AA', 'UK');

The SQL UPDATE


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

Note: Be careful when updating records in a table! Notice


the WHERE clause in the UPDATE statement.
The WHERE clause specifies which record(s) that should be
updated. If you omit the WHERE clause, all records in the
table will be updated!
UPDATE Customers
SET ContactName = 'Alfred Schmidt',
City= 'Frankfurt'
WHERE CustomerID = 1;
UPDATE Multiple Records
It is the WHERE clause that determines how many records
will be updated.

The following SQL statement will update the ContactName


to "Juan" for all records where country is "Mexico":

Example
UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';

The SQL DELETE


Statement
The DELETE statement is used to delete existing records
in a table.

DELETE FROM Customers WHERE CustomerName='Alfreds


Futterkiste';

Delete All Records


It is possible to delete all rows in a table without deleting
the table. This means that the table structure, attributes,
and indexes will be intact:
DELETE FROM table_name;
DELETE FROM Customers;

Delete a Table
To delete the table completely, use the DROP
TABLE statement:

DROP TABLE Customers;

The 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.

For minimum price:

SELECT MIN(Price)
FROM Products;

For Maximum Price:

SELECT MAX(Price)
FROM Products;
The SQL COUNT() Function
The COUNT() function returns the number of rows that
matches a specified criterion.

Find the total number of rows in the Products table:


SELECT COUNT(*)
FROM Products;

The SQL SUM() Function


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

Return the sum of all Quantity fields in


the OrderDetails table:
SELECT SUM(Quantity)
FROM OrderDetails;

Add a WHERE Clause


You can add a WHERE clause to specify conditions:

Example
Return the sum of the Quantity field for the product
with ProductID 11:
SELECT SUM(Quantity)
FROM OrderDetails
WHERE ProductId = 11;

Use SUM() with GROUP BY


Here we use the SUM() function and the GROUP BY clause,
to return the Quantity for each OrderID in the
OrderDetails table:

Example
SELECT OrderID, SUM(Quantity) AS [Total Quantity]
FROM OrderDetails
GROUP BY OrderID;

SUM() With an Expression


The parameter inside the SUM() function can also be an
expression.

If we assume that each product in


the OrderDetails column costs 10 dollars, we can find
the total earnings in dollars by multiply each quantity with
10:

Example
Use an expression inside the SUM() function:
SELECT SUM(Quantity * 10)
FROM OrderDetails;
The SQL AVG() Function
The AVG() function returns the average value of a
numeric column.

Find the average price of all products:


SELECT AVG(Price)
FROM Products;

The SQL LIKE Operator


The LIKE operator is used in a WHERE clause to search for
a specified pattern in a column.

There are two wildcards often used in conjunction with


the LIKE operator:

 The percent sign % represents zero, one, or multiple


characters
 The underscore sign _ represents one, single
character

Select all customers that starts with the letter "a":


SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';
The SQL IN Operator
The IN operator allows you to specify multiple values in
a WHERE clause.

The IN operator is a shorthand for multiple OR conditions.

Return all customers from 'Germany', 'France', or 'UK'


SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');

The SQL BETWEEN


Operator
The BETWEEN operator selects values within a given range.
The values can be numbers, text, or dates.

The BETWEEN operator is inclusive: begin and end values


are included.

Selects all products with a price between 10 and 20:


SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;

SQL JOIN
A JOIN clause is used to combine rows from two or more
tables, based on a related column between them.
Let's look at a selection from the "Orders" table:

OrderID CustomerID OrderDate

10308 2 1996-09-18

10309 37 1996-09-19

10310 77 1996-09-20

Then, look at a selection from the "Customers" table:

CustomerID CustomerName ContactName

1 Alfreds Futterkiste Maria Anders

2 Ana Trujillo Emparedados y helados Ana Trujillo

3 Antonio Moreno Taquería Antonio Moreno


Notice that the "CustomerID" column in the "Orders" table
refers to the "CustomerID" in the "Customers" table. The
relationship between the two tables above is the
"CustomerID" column.

Then, we can create the following SQL statement (that


contains an INNER JOIN), that selects records that have
matching values in both tables:

SELECT Orders.OrderID, Customers.CustomerName,


Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Custome
rs.CustomerID;

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
The SQL GROUP BY
Statement
The GROUP BY statement groups rows that have the same
values into summary rows, like "find the number of
customers in each country".

The GROUP BY statement is often used with aggregate


functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to
group the result-set by one or more columns.

The following SQL statement lists the number of


customers in each country:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
The SQL HAVING Clause
The HAVING clause was added to SQL because
the WHERE keyword cannot be used with aggregate
functions.

The following SQL statement lists the number of


customers in each country. Only include countries with
more than 5 customers:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;

The following SQL statement lists the number of


customers in each country, sorted high to low (Only
include countries with more than 5 customers):

Example
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy