SQL Is A Standard Language For Accessing and Manipulating Databases. What Is SQL?
SQL Is A Standard Language For Accessing and Manipulating Databases. What Is SQL?
SQL Is A Standard Language For Accessing and Manipulating Databases. What Is SQL?
What is SQL?
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the
International Organization for Standardization (ISO) in 1987
Although SQL is an ANSI/ISO standard, there are different versions of the SQL language.
However, to be compliant with the ANSI standard, they all support at least the major commands (such
as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.
The owner of the online store uses structured query language (SQL) to get information from the
database.
Give three reasons why SQL is used to manipulate data in databases.
CUSTOMER Table
SELECT Statement
SELECT Column Example
SELECT CustomerName, City FROM Customers;
SELECT * Example
SELECT * FROM Customers;
SELECT Country FROM Customers;
WHERE Syntax
SELECT * FROM Customers WHERE Country='Mexico';
SELECT * FROM Customers WHERE CustomerID=1;
OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
ORDER BY DESC
SELECT * FROM Customers ORDER BY Country DESC;
ROUND() Function
ROUND(number, decimals)
SELECT ROUND(235.415, 2)
SUM() Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition;
MAX() Syntax
SELECT MAX(column_name)
FROM table_name
WHERE condition;
AVG() Syntax
SELECT AVG(column_name)
FROM table_name
WHERE condition;
GROUP BY Statement
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
NULL Values
A field with a NULL value is a field with no value
IS NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
SELECT CustomerName, ContactName, Address FROM Customers WHERE Address IS NULL;
SELECT CustomerName, ContactName, Address FROM Customers WHERE Address IS NOT NULL;
Note : Be careful when updating records. If you omit the WHERE clause, ALL records will be updated!
DELETE Statement
DELETE FROM table_name WHERE condition;
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';
16. In products table, update the product_name to 'Grapefruit' for all records whose product_name is
"Apple".
17. In suppliers table populated with the following data, update the city to 'Boise' and the state to
"Idaho" for all records whose supplier_name is "Microsoft".
18. The first_name field is set to 'Judy' in the customers table where the customer_id is equal to 8000.
19. In the supplier table, update the supplier_id to 150, the supplier_name to 'Apple' and city to
'Cupertino' where the supplier_name is 'Google'.
SELECT TOP
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;
LIMIT
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
ROWNUM
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;
ADD a WHERE
SELECT TOP 3 * FROM Customers WHERE Country='Germany';
SELECT * FROM Customers WHERE Country='Germany' LIMIT 3;
SELECT * FROM Customers WHERE Country='Germany' AND ROWNUM <= 3;
LIKE Operator
There are two wildcards often used in conjunction with the LIKE operator:
(1) % - The percent sign represents zero, one, or multiple characters
(2) _ - The underscore represents a single character
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
SELECT * FROM Customers WHERE CustomerName LIKE 'a%'; [CustomerName ending with "a":]
SELECT * FROM Customers WHERE CustomerName LIKE '%a'; [CustomerName that have "or" in any
position]
SELECT * FROM Customers WHERE CustomerName LIKE '%or%'; [CustomerName that have "or" in
any position]
SELECT * FROM Customers WHERE CustomerName LIKE '_r%'; [CustomerName that have "r" in the
second position]
SELECT * FROM Customers WHERE CustomerName LIKE 'a__%'; [CustomerName that starts with
"a" and are at least 3 characters in length]
SELECT * FROM Customers WHERE ContactName LIKE 'a%o'; [ContactName that starts with "a"
and ends with "o]
NOT LIKE
SELECT * FROM Customers WHERE CustomerName NOT LIKE 'a%'; [CustomerName that does NOT
start with "a"]
Wildcard Characters
Using the % Wildcard
SELECT * FROM Customers WHERE City LIKE 'ber%'; [City starting with "ber"]
SELECT * FROM Customers WHERE City LIKE '%es%'; [City containing the pattern "es"]
[charlist] Wildcard
SELECT * FROM Customers WHERE City LIKE '[bsp]%'; [City starting with "b", "s", or "p]
SELECT * FROM Customers WHERE City LIKE '[a-c]%'; [City starting with "a", "b", or "c"]
[!charlist] Wildcard
SELECT * FROM Customers WHERE City LIKE '[!bsp]%'; [City NOT starting with "b", "s", or "p"]
SELECT * FROM Customers WHERE City NOT LIKE '[bsp]%';
IN Operator
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
SELECT * FROM Customers WHERE Country IN ('Germany', 'France', 'UK'); [customers that are
located in "Germany", "France" or "UK"]
SELECT * FROM Customers WHERE Country NOT IN ('Germany', 'France', 'UK'); [customers that are
NOT located in "Germany", "France" or "UK"]
SELECT * FROM Customers WHERE Country IN (SELECT Country FROM Suppliers); [all customers
that are from the same countries as the suppliers]
20. Display all records who’s name starts with A and ends with n
SELECT * FROM CUSTOMER WHERE CustomerName LIKE “A%n”;
21. Display all records who’s name have no ( case sensitive )
SELECT * FROM customer WHERE name LIKE binary ‘%no%’;
22. Display all records who got mark in nineties but not equal to 100
23. Display all records who have alex or deo any where in name column
24. Display all records who have alex and Jon any where in name column
25. Display all records who have ro any where in name column and got mark in nineties
26. Retrieve the prod_id and prod_name fields from a table named Products if the prod_name contains
the string 'bean bag'
27. Select the columns prod_id and prod_name from the Products table. Return the rows whose
prod_name begins with the string 'Fish' and is followed by 0 or more occurrences of any character.
28. Select the column prod_name from the Products table. Return the rows whose prod_name begins
with any number of occurences of any character, followed by 'bean b', followed by a single instance
of any character, followed by 'g', followed by any number of occurrences of any character.
NOT BETWEEN
SELECT * FROM Products WHERE Price NOT BETWEEN 10 AND 20;
BETWEEN with IN
SELECT * FROM Products WHERE Price BETWEEN 10 AND 20 AND CategoryID NOT IN (1,2,3);
BETWEEN Dates
SELECT * FROM Orders WHERE OrderDate BETWEEN #01/07/1996# AND #31/07/1996#;
SELECT * FROM Orders WHERE OrderDate BETWEEN '1996-07-01' AND '1996-07-31';
Alias Column Syntax (to give a table, or a column in a table, a temporary name)
SELECT column_name AS alias_name
FROM table_name;
SELECT CustomerID AS ID, CustomerName AS Customer FROM Customers;
CustomerName Address
SQL JOIN
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate FROM Orders INNER JOIN
Customers ON Orders.CustomerID=Customers.CustomerID;
INNER JOIN
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
SELECT Orders.OrderID, Customers.CustomerName FROM Orders INNER JOIN Customers ON
Orders.CustomerID = Customers.CustomerID;
LEFT JOIN
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
RIGHT JOIN
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
SELECT Orders.OrderID, Employees.LastName, Employees.FirstName FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID ORDER BY
Orders.OrderID;
Self JOIN
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
30. Write a query in SQL to display the first name, last name, department number, and department
name for each employee.
31. Write a query in SQL to display the first and last name, department, city, and state province for each
employee.
32. Write a query in SQL to display the first name, last name, department number and department
name, for all employees for departments 80 or 40 order by Last_name.
SELECT e.first_name, e.Last_name, d.department_id, d.department_name
33. Write a query in SQL to display those employees who contain a letter z to their first name and also
display their last name, department, city, and state province.
SELECT e,First_name, e.Last_name, d.department_id, l.city, l.State_province
FROM Employees AS e
INNER JOIN Department AS d
ON e.department_id=d.department_id
INNER JOIN Location AS l
ON d.location_id = l.location_id
WHERE e.Last_name LIKE “%z%”;
34. Write a query in SQL to display all departments including those where does not have any employee.
SELECT e.fist_name,
e.last_name,
d.department_name
FROM department d
LEFT JOIN employees e
ON e.department_id = d.department_id
35. Write a query in SQL to display the department name, city, and state province for each department.
36. Write a query in SQL to display the first name, last name, department number and name, for all
employees who have or have not any department.
37. Write a query in SQL to display the first name of all employees and the first name of their manager
including those who does not working under any manager.
SELECT e1.first_name AS "employee_name",
e2.first_name AS "manager_name"
FROM employees e1
LEFT JOIN employees e2
ON e1.manager_id = e2.employee_id;
38. Write a query in SQL to display the first name, last name, and department number for those
employees who works in the same department as the employee who holds the last name as Taylor.
SELECT e1.first_name,
e1.last_name,
e1.department_id
FROM employees e1
INNER JOIN employees e2
ON e1.department_id = e2.department_id
AND e2.last_name = 'Taylor';
39. Write a query in SQL to display the job title, department name, full name (first and last name ) of
employee, and starting date for all the jobs which started on or after 1st January, 1993 and ending
with on or before 31 August, 1997
SELECT j.job_title,
d.department_name,
CONCAT(e.first_name, ' ', e.last_name) AS full_name,
jh.start_date
FROM employees e
INNER JOIN job_history jh
ON e.employee_id = jh.employee_id
AND jh.start_date BETWEEN '1993-01-01' AND '1997-08-31'
INNER JOIN jobs j
ON jh.job_id = j.job_id
INNER JOIN departments d
ON jh.department_id = d.department_id;
40. Write a query in SQL to display job title, full name (first and last name ) of employee, and the
difference between maximum salary for the job and salary of the employee.
SELECT j.job_title,
CONCAT(e.first_name, ' ', e.last_name) AS full_name,
(j.max_salary - e.salary) AS salary_diff
FROM employees e
INNER JOIN jobs j
ON e.job_id = j.job_id;
41. Write a query in SQL to display the name of the each department, average salary and number of
employees working in that department who got commission.
SELECT d.department_name,
AVG (e.salary),
COUNT (e. commission_pct)
FROM employees e
INNER JOIN department d
ON e.depatment_id = d.department_id
GROUP BY (d,department_name)
42. Write a query in SQL to display the full name (first and last name) of employees, job title and the
salary differences to their own job for those employees who is working in the department ID 80.
43. Write a query in SQL to display the name of the country, city, and the departments which are
running there that means same country and same location.
SELECT d.department_name,
l.city,
c.county
FROM department d
INNER JOIN location l
ON d.location_id = l.location_id
INNER JOIN countries c
ON l.country_id = c.country_id
44. Write a query in SQL to display job title and average salary of employees.
45. Employees and departments (Employees & Departments tables)
For each employee, display the first name, last name, department number and department name.
Display the first name, last name, department number and department name, for all employees in
departments 50 or 90.
46. Departments and locations (Departments, Employees & Locations tables)
For each department, display the department name, city, and state province.
For each employee, display the full name, department name, city, and state province.
Display the full name, department name, city, and state province, for all employees whose last name
contains the letter a.
UNION Operator
The UNION operator is used to combine the result-set of two or more SELECT statements.
Each SELECT statement within UNION must have the same number of columns
The columns must also have similar data types
The columns in each SELECT statement must also be in the same order
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
SELECT City FROM Customers UNION SELECT City FROM Suppliers ORDER BY City;
SELECT City FROM Customers UNION ALL SELECT City FROM Suppliers ORDER BY City;
HAVING
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
EXISTS Operator
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
SELECT SupplierName FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID =
Suppliers.supplierID AND Price < 20);
SELECT ProductName
FROM Products
WHERE ProductID = ANY (SELECT ProductID FROM OrderDetails WHERE Quantity = 10);
SELECT ProductName
FROM Products
WHERE ProductID = ANY (SELECT ProductID FROM OrderDetails WHERE Quantity > 99);
SELECT * INTO CustomersGermany FROM Customers WHERE Country = 'Germany'; [copies only
the German customers into a new table]
SELECT INTO can also be used to create a new, empty table using the schema of another. Just add a
WHERE clause that causes the query to return no data
SELECT * INTO newtable
FROM oldtable
WHERE 1 = 0;
Copies "Suppliers" into "Customers" (the columns that are not filled with data, will contain NULL)
INSERT INTO Customers (CustomerName, City, Country) SELECT SupplierName, City, Country
FROM Suppliers;
Copies "Suppliers" into "Customers" (fill all columns) INSERT INTO Customers (CustomerName,
ContactName, Address, City, PostalCode, Country) SELECT SupplierName, ContactName, Address,
City, PostalCode, Country FROM Suppliers;
Copies only the German suppliers into "Customers" INSERT INTO Customers (CustomerName, City,
Country) SELECT SupplierName, City, Country FROM Suppliers WHERE Country='Germany';
OR
CREATE TABLE Persons (
ID int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);