Download as pdf
Download as pdf
You are on page 1of 11
Introduction to SQL In this chapter, we learn the SQL through waschools website: What is SQL? SQL stands for Structured Query Language. It is a standard language for accessing and manipulating databases. King’s College Slivester Lee + SQL can execute queries against a database Introduction to SQL + SOL can retrieve data from a database + SQL can insert records in a database . RDBMS + SQL ean update reords ina database Introduction Bole dene + RDBMS stands for Relational Database Management System aoe to SQL + RDBMS isthe bass for SQL, and forall modern database systems such as = MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access. + SQL can create new tables in a database = SQL can create stored procedures in a database Raenais sk ce = SQL can create views in a database + SQL can set permissions on tables, procedures, and views ones SQL Syntax os tno eGo Pee tte teen mie nt evs nina centsns fe ecrda one fo ech custo) a st seven scum Cuno, Caseraane, Contato, Ades, iy, PstaCade aCe) Introduction to SQL 2S * SQL keywords are NOT case sensitive: select is the same as SELECT | Sasori eer * Some database systems require a semicolon at the end of each SQL statement. + Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server. SQL Syntax SQL statements + Most of the actions you need to perform on a database are done with SQL statements, + The following SQL statement selects all the records in the “Customers” table: SQL SELECT Statement [The SOL SELECT statement | + The SELECT statement is used to select data from a databs ‘The data returned is stored in a result table, called the re SELECT Syntax SELECT columni, columnd, FROM table_name, you want to select data from, the folowing syntax: — SQL SELECT Statement Below is a selection Below is from the "Customers" table in the Northwind sample : Ande rasate pen ei an ci 2 ‘soa tte trguetnony fouTae—frin es Conmca sco 0521 etn ee 5 Sopindmatiig ea i ‘seam ‘Sept at ome SQL SELECT DISTINCT Statement Pra Saree cw crme es + The SELECTDISTINCT statement is used to return only distinct (different) values. + Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values. SELECT DISTINCT Syntax SQL SELECT Statement SELECT Column Example ing SQL statement selects the “CustomerName” and "City" columns 3" table: SQL SELECT DISTINCT Statement SELECT Example Without DISTINCT lowing SQL statement selects all (including the duplicates) values from the "Country* column in the “Customers” table: SGI SELECT DISTINCT Statement SQL SELECT DISTINCT Statement SELECT DISTINCT Examples + The following SQL stat “Country” col selects only the DISTINCT values from the SELECT DISTINCT Examples ‘Note: The example of previous slide will not work in Firefox! Because COUNTIDISTINCT cofumn_name) is not supported in Microsoft Access databases. Firefox is using Microsoft Access in our examples, * The following SQL statement lists the number of diffe countries: SQL WHERE Clause sae HE Cause FLEXOR + The WHERE clause is used to filter records. + Ieis used to extract only those records that fulfill a specified condition, — Note: The WHERE clause is not only used in SELECT statements, it is also used in UPDATE, DELETE, ete! WHERE Syntax WHERE Cust —S + The folowing operators can be used in the WHERE clause: SQL AND, OR and NOT Operators PURO e eet nomen : | SQL WHERE 2 The OR operator displays a record if any of the conditions separated by OR is TRUE. «The NOT operator displays a record if the condition(s) is NOT TRUE. SQL AND, OR and NOT Operators AND Example + The folowing SQL statement see cts al fields from “Customers” where country is “Germany” AND city Benin” * Baample x SELECT * FROM Customers cz WHERE Country= Germany AND City="Rerlin’ ers" where country NOT \losed in quotes: SQL AND, OR and NOT Operators OR Example +The following SQL statement selects all fields from "Customers' where city is "Berlin Maange HE SEG [setter FROM Customers + The following SQL country is "Germs [ence cree Sates on ee a SQL ORDER BY Keyword Bose rece * The QRDERIBY keyword is used to sort the result-set in ascending or descending order. * The ORDERBY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword, ORDER BY Syntax [SELECT column, abun, PROM table name [ORDER BY column, cobumn?, .. ASCIDESC; SQL AND, OR and NOT Operators Combining AND, OR and NOT + You can also combine the AND, OR and NOT operators. «The following SQL statement selects all fields from “Customers” where country is “Germany” AND city must be ‘Berlin’ OR "Munchen" (use parenthesis to form complex ‘SELECT * FROM Customers WHERE NOT Country~Germany AND NOT Countsy"USA', SQL ORDER BY Keyword ORDER BY Example + The following SOL statement selects all customers from the “Customers* table, sorted by the "County? column: Toeaple: oie ‘SELECT * FROM Customers ORDER BY Country, ORDER BY DESC Example ‘Tha folowing SOL statement selects all customers from the “Customers” table, sorted DESCENDING by the “Country” column: Example: SELECT * FROM Customers ORDER BY Country DESC. SQL ORDER BY Keyword ORDER BY Several Columns Example + The following SOL statement select a SQL MIN() and MAX() Functions Boer ns nctions Below is a selection from the "Products" table in the Northwind sample database: Productid Productiame SupptlertD CategeryID Unit Pron a ome cone ‘Actes yb ‘enat Anson's Can Seasoning Gia al cee mc ass SQL COUNT(), AVG() and SUM() Functions SQL COUNT(), AVG() and SUM() Functions Sn ee eee Be anne oe ee bas SUM) Syntax ae COUNTY) Syntax AVG) Syntax +The AVGH function returns the average value ofa numeric column, SELECT AVO|cokumm name) FROM table name SQL COUNT(), AVG() and SUM() Functions COUNT() Example + The folowing SQL statement finds the numberof products; SELECT COUNT(Produ || FROM Products, ote: NULL values ar ignored, + The SUM function returns the total sum of a numeric column, ‘SELECT SUM(column_name) FROM table name WHERE condition; ee Demo Database * Below isa selection from the "Products" table in the Northwind sample database: JuctO ProductName Supplerio CategoryIO Unk Price ‘oie 4 a sobows x20 begs 18 ong 1 : 2-12erbeties 19 ‘ee Spr : 2 ‘et Aros Cain Seasoning 2 2 (het Area's Gumbo Mie a SQL COUNT(), AVG() and SUM() Functions Demo Database ~ Below isa selection from the "OrderDetails table in the Northwind sample database uantity) FROM OrderDetails; Note: NULL values are ignored, N SQL IN Operator SQL IN Operator Ee nanos * The IN operator allows you to specify multiple values in a WHERE clause = The IN operator is a shorthand for multiple OR conditions. SQL BETWEEN Operator Demo Database SQL BETWEEN Operator De oF ea eed «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. BETWEEN Syntax “SELECT cofumn_namels) FROM table-name WHERE column name BETWEEN value! AND valued; with a price between 10 and 20: lecte all product AND 20; SQL BETWEEN Operator SQL BETWEEN Operator NOT BETWEEN Ex, BETWEEN Text Value Example BETWEEN Example + To display the products soca tae [seuzer Fron Produce WHERE Price NOT BETWI * Th flowing SOL statement scict ll products witha ProductName between Carmarvon Tigera sd Cet Antons Cajun Seasoning am SQL BETWEEN Operator NOT BETWEEN Text Value Example seats 8 Selection from the "Orders" table in the Northwind sample database OrdertD ——CustomertD EmployeerD Shipperto, 0298 %0 5 3 10249 a ‘ 1 » + 2 os 2 1 % 4 2 + The otowing Sol. '31-July-| oo END | WHERE OrderDate BETWEEN "1996.07.01 AES a oat

You might also like