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

Structured Query Language SQL Queries

The document provides an overview of SQL queries, including their purpose and various types of operators such as comparison, arithmetic, and logical operators. It details how to perform operations like SELECT, INSERT, UPDATE, DELETE, CREATE TABLE, and ALTER TABLE with examples. Additionally, it covers the COUNT function for aggregating data based on specific conditions.

Uploaded by

Roel Bergado
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Structured Query Language SQL Queries

The document provides an overview of SQL queries, including their purpose and various types of operators such as comparison, arithmetic, and logical operators. It details how to perform operations like SELECT, INSERT, UPDATE, DELETE, CREATE TABLE, and ALTER TABLE with examples. Additionally, it covers the COUNT function for aggregating data based on specific conditions.

Uploaded by

Roel Bergado
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

STRUCTURED

QUERY LANGUAGE
(SQL QUERIES)
SQL queries are commands written in Structured Query
Language (SQL) that allow users to interact with databases. SQL
queries are used to perform various operations, such as retrieving,
inserting, updating, and deleting data.
SQL OPERATORS
 In SQL, operators are symbols or keywords used to perform various
operations on data. These operations include comparisons, arithmetic
calculations, logical evaluations, and more.
SQL COMPARISON
OPERATORS
 =: Checks if two values are equal.
 Example: WHERE Age = 30

 != or <>: Checks if two values are not equal.


 Example: WHERE Age != 30

 >: Greater than.


 Example: WHERE Salary > 50000

 <: Less than.Example: WHERE Salary < 50000


 >=: Greater than or equal to.
 Example: WHERE Age >= 18
SQL COMPARISON
OPERATORS
 <=: Less than or equal to.
 Example: WHERE Age <= 65

 BETWEEN: Checks if a value falls within a range.


 Example: WHERE Age BETWEEN 18 AND 30

 LIKE: Used for pattern matching.


 Example: WHERE FirstName LIKE ‘A*’

 IN: Checks if a value exists in a list of values.


 Example: WHERE Country IN ('USA', 'Canada', 'UK')IS

 NULL: Checks for NULL values (missing data).


 Example: WHERE LastName IS NULL
SQL COMPARISON
OPERATORS
 These are used in the WHERE clause to compare two values.
SQL COMPARISON
OPERATORS
 AND: Combines conditions and returns true if all conditions are true.
 Example: WHERE Age > 18 AND City = 'New York’

 OR: Combines conditions and returns true if at least one condition is true.
 Example: WHERE Age < 18 OR Age > 65

 NOT: Negates a condition.


 Example: WHERE NOT (City = 'New York')
SQL ARITHMETIC
OPERATORS
 These operators perform mathematical operations on numeric values.
SQL ARITHMETIC
OPERATORS
 +: Addition.
 Example: SELECT Salary + 5000 FROM Employees

 -: Subtraction.
 Example: SELECT Salary - 2000 FROM Employees

 *: Multiplication.
 Example: SELECT Salary * 2 FROM Employees

 /: Division.
 Example: SELECT Salary / 2 FROM Employees

 %: Modulus (returns the remainder of a division).


 Example: SELECT Salary % 3 FROM Employees
SELECT QUERY
 SELECT query is used to retrieve data from one or more tables. It allows
you to specify criteria to filter, sort, or group the data.
 Syntax:
 SELECT column1, column2, ...
 FROM table_name
 WHERE condition;
SELECT QUERY
 EXAMPLE:
 SELECT *
 FROM tblUsers
 WHERE Gender = 'Male';
INSERT INTO QUERY
 This is used to add new records to a table.
INSERT INTO QUERY
 INSERT INTO table_name (column1, column2, ...)
 VALUES (value1, value2, ...);

 EXAMPLE:
 INSERT INTO tblUsers (fName, mName, lName, Gender, Age, bMonth, bDay,
bYear)
 VALUES ('Carlo', 'Bulan', 'Corpuz', 'Male', 17, 11, 9, 1997);
UPDATE QUERY
 UPDATE table_name
 SET column1 = value1, column2 = value2, ...
 WHERE condition;

 EXAMPLE:
 UPDATE tblUsers
 SET Age = 18, bMonth = 12
 WHERE fName = 'Carlo' AND mName = 'Bulan' AND lName = 'Corpuz';
DELETE QUERY
 This is used to delete records from a table.
DELETE QUERY
 DELETE FROM table_name
 WHERE condition;

 EXAMPLE:

DELETE FROM tblUsers


WHERE ID1 = 54;
CREATE TABLE QUERY
 CREATE TABLE table_name (
 column1 data_type1,
 column2 data_type2,
 ...
 );
CREATE TABLE QUERY
 EXAMPLE:
 CREATE TABLE tblEmployees (
 EmployeeID AUTOINCREMENT PRIMARY KEY,
 fName TEXT(255),
 mName TEXT(255),
 lName TEXT(255),
 Gender TEXT(10),
 Age INTEGER,
 bMonth INTEGER,
 bDay INTEGER,
 bYear INTEGER
 );
ALTER TABLE QUERY
 The ALTER TABLE statement in Microsoft Access allows you to modify an
existing table by adding, modifying, or deleting columns, or altering column
data types.
ALTER TABLE QUERY
 Adding a New Column
 ALTER TABLE table_name
 ADD column_name data_type;

 EXAMPLE:
 ALTER TABLE tblUsers
 ADD Email TEXT(255);
ALTER TABLE QUERY
 Modifying an Existing Column (Changing Data Type)
 ALTER TABLE table_name
 ALTER COLUMN column_name new_data_type;

 EXAMPLE:
 ALTER TABLE tblUsers
 ALTER COLUMN Gender TEXT(20);
ALTER TABLE QUERY
 Deleting a Column
 ALTER TABLE table_name
 DROP COLUMN column_name;

 EXAMPLE:
 ALTER TABLE tblUsers
 DROP COLUMN mName;
COUNT QUERY
 The COUNT() function returns the number of rows that match a specified
condition.

 SELECT COUNT(*) AS TotalRecords


 FROM tblUsers;
COUNT QUERY
 To count unique entry:
 SELECT COUNT(DISTINCT fName) AS UniqueFirstNames FROM tblUsers;
COUNT QUERY
 Count with a Condition
 SELECT COUNT(*) AS TotalMales FROM tblUsers WHERE Gender = 'Male';
COUNT QUERY
 Count by Group
 SELECT Gender, COUNT(*) AS TotalByGender FROM tblUsers GROUP BY
Gender;
COUNT QUERY
 Count with Multiple Conditions (HAVING Clause)
 SELECT Age, COUNT(*) AS TotalByAge
 FROM tblUsers
 GROUP BY Age
 HAVING COUNT(*) > 1; -- Only show ages with more than 1 user

You might also like