Structured Query Language – The Basics

Slides:



Advertisements
Similar presentations
Basic SQL Introduction Presented by: Madhuri Bhogadi.
Advertisements

Virtual training week 4 structured query language (SQL)
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Introduction to Structured Query Language (SQL)
Structured Query Language Part I Chapter Three CIS 218.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi Information Systems Spring 2011.
SQL Tutorials To understand some of the topics please analyze the following tutorials: The following tutorials will help:
Structured Query Language(SQL) XU Yinqing SEEM PHD YEAR 3.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Structured Query Language. SQL is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database systems.
Database A collection of related data. Database Applications Banking: all transactions Airlines: reservations, schedules Universities: registration, grades.
Sundara Ram Matta Apr 01 st, Sundara Ram Matta Apr 01 st, 2015
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Using Special Operators (LIKE and IN)
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
SQL Unit – 2 Base Knowledge Presented By Mr. R.Aravindhan.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
CHAPTER 9 SQL อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
More queries Outer joins and summary queries. Inner and outer joins An Inner join only returns matching rows from two tables –E.g. if I join the customer.
INFANL01-3 ANALYSE 3 WEEK 3 March 2015 Institute voor Communication, Media en Informatietechnology.
IS6146 Databases for Management Information Systems Lecture 4: SQL IV – SQL Functions and Procedures Rob Gleasure robgleasure.com.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
SQL Structured Query Language. SQL is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
Fundamental of Database Systems
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Web Systems & Technologies
Module T03d Software Engineering
CHAPTER 7 DATABASE ACCESS THROUGH WEB
SQL Query Getting to the data ……..
Rob Gleasure robgleasure.com
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Introduction to Structured Query Language(SQL)
SQL Implementation & Administration
Module 10: Implementing Triggers
MySQL Joins MySQL joins are used to combine rows from two or more tables. Different SQL JOINs INNER JOIN: Returns all rows when there is at least one match.
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Using SQL to Prepare Data for Analysis
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
MENAMPILKAN DATA DARI SATU TABEL (Chap 2)
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Rob Gleasure robgleasure.com
SQL LANGUAGE and Relational Data Model TUTORIAL
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Introduction To Structured Query Language (SQL)
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Database systems Lecture 3 – SQL + CRUD
SQL Queries Chapter No 3.
Structured Query Language – The Fundamentals
Introduction To Structured Query Language (SQL)
Structured Query Language
SQL Tutorial w3schools.
Presentation transcript:

Structured Query Language – The Basics SQL Structured Query Language – The Basics By Hao Nguyen 1

What We Are Going to Cover Overview of SQL Data Definition Language Creating tables Data Manipulation Language Inserting / Updating / Deleting data Retrieving data Single table queries Where Joins Grouping

SQL SQL is a data manipulation language. SQL is not a programming language. SQL commands are interpreted by the database management system (DBMS) engine. SQL commands can be used interactively as a query language within the DBMS. SQL commands can be embedded within programming languages. A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders").  Tables contain records (rows) with data.

3 Types of SQL Commands Data Definition Language (DDL): Commands that define a database - Create, Alter, Drop Data Manipulation Language (DML) – main one to cover today Commands that maintain and query a database. Data Control Language (DCL) Commands that control a database, including administering privileges and committing data. Keep in Mind That... SQL is NOT case sensitive: select is the same as SELECT In this tutorial we will write all SQL keywords in upper-case.

Most Comman 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

Demo Database We will use the well-known Northwind sample database, included with MS Acess and MS SQL Server Tables: Customers Categories Employees OrderDetails Orders Products Shippers Suppliers

Inserting Data into a Table INSERT INTO tablename> (comma seperated column-list) VALUES (comma seperated value-list) Example Categories table: CategoryID CategoryName Description 1 Beverages Soft drinks, coffees, teas, beers, and ales 2 Condiments Sweet and savory sauces, relishes, spreads, and seasonings 3 Confections Desserts, candies, and sweet breads 4 Dairy Products Cheeses 5 Grains/Cereals Breads, crackers, pasta, and cereal 6 Meat/Poultry Prepared meats 7 Produce Dried fruit and bean curd 8 Seafood Seaweed and fish

Inserting Data into a Table Example: Insert a new row into CATEGORIES table Insert INTO Categories (CategoryID, CategoryName, Description) Values (9, 'Dry/Baked Goods','Sugar, Flour, Spices'); Categories table will now look like this: CategoryID CategoryName Description 1 Beverages Soft drinks, coffees, teas, beers, and ales 2 Condiments Sweet and savory sauces, relishes, spreads, and seasonings 3 Confections Desserts, candies, and sweet breads 4 Dairy Products Cheeses 5 Grains/Cereals Breads, crackers, pasta, and cereal 6 Meat/Poultry Prepared meats 7 Produce Dried fruit and bean curd 8 Seafood Seaweed and fish 9 Dry/Baked Goods Sugar, Flour, Spices

Deleting Data DELETE FROM table_name WHERE some_column=some_value; Online SQL Editor with demo database: Notice the WHERE clause in the SQL DELETE statement! The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted! Example code: DELETE FROM Categories WHERE CategoryID=9 http://www.w3schools.com/sql/trysql.asp?filename=trysql_delete

Select Statement The SELECT statement is used to select data from a database. The result is stored in a result table, called the result-set. SQL SELECT Syntax SELECT column_name1,column_name2 FROM table_name; and SELECT * FROM table_name;

Select Examples SELECT Column Example SELECT CustomerName, City The following SQL statement selects the "CustomerName" and "City" columns from the "Customers" table: SELECT CustomerName, City FROM Customers; SELECT * Example The following SQL statement selects all the columns from the “Customers” table: SELECT * FROM Customers http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_columns

SQL SELECT DISTINCT Statement In a table, a column may contain many duplicate values; and sometimes you only want to list the different (distinct) values. The DISTINCT keyword can be used to return only distinct (different) values. SQL SELECT DISTINCT Syntax: SELECT DISTINCT column_name1,column_name2 FROM table_name; Example: SELECT DISTINCT City, State FROM Customers; http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_columns

SQL Where Clause The WHERE clause is used to extract only those records that fulfill a specified criterion, i.e. filter out records SQL WHERE Syntax SELECT column_name1,column_name2 FROM table_name WHERE column_name operator value; Example SELECT * FROM Customers WHERE City= ‘London’;

SQL WHERE Clause Text Fields vs. Numeric Fields Example SQL requires single quotes around text-values (e.g., ‘London’) Some databases will also allow double quotes Numeric fields should not be enclosed in quotes Example SELECT * FROM Customers WHERE CustomerID=1;

SQL WHERE Clause Operators in the WHERE Clause Operator Description = Equal <> Not equal. Note: In some versions of SQL this operator may be written as != > Greater than < Less than >= Greater than or equal <= Less than or equal BETWEEN Between an inclusive range LIKE Search for a pattern IN To specify multiple possible values for a column

SQL Joins SQL joins are used to combine rows from two or more tables, based on a common field between them (Primary Key) Types of SQL Joins INNER JOIN: Returns all rows when there is at least one match in BOTH tables LEFT JOIN: Return all rows from the left table, and the matched rows from the right table RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table FULL JOIN: Return all rows when there is a match in ONE of the tables

SQL Joins http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins

SQL Joins The most common type of join is: SQL INNER JOIN (simple join). An SQL INNER JOIN return all rows from multiple tables where the join condition is met. Let’s look at a selection from the ‘Orders’ table: Now have a look at a selection from ‘Customers’ table: OrderID CustomerID OrderDate 10308 2 1996-09-18 10365 3 1996-11-27 10355 4 1996-11-15 CustomerID CustomerName ContactName Country 1 Alfreds Futterkiste Maria Anders Germany 2 Ana Trujillo Emparedados y helados Ana Trujillo Mexico 3 Antonio Moreno Taquería Antonio Moreno 4 Around the Horn Thomas Hardy UK

SQL Join The primary key between the two tables is the ‘CustomerID’ column. We can run a SQL statement that joins these two tables based on the ‘CustomerID’ field Example: SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate FROM Orders INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID; OrderID CustomerName OrderDate 10308 Ana Trujillo Emparedados y helados 9/18/1996 10365 Antonio Moreno Taquería 11/27/1996 10383 Around the Horn 12/16/1996 10355 11/15/1996 10278 Berglunds snabbköp 8/12/1996

SQL Functions SQL has many built-in functions for performing calculations on data. SQL Aggregate Functions SQL aggregate functions return a single value, calculated from values in a column. Useful aggregate functions: COUNT() - Returns the number of rows SUM() - Returns the sum AVG() - Returns the average value FIRST() - Returns the first value LAST() - Returns the last value MAX() - Returns the largest value MIN() - Returns the smallest value

SQL COUNT() Function SQL COUNT(column_name) Syntax: The COUNT(column_name) function returns the number of values (NULL values will not be counted) of the specified column: SELECT COUNT(column_name) FROM table_name; SQL COUNT(*) Syntax: The COUNT(*) function returns the number of records in a table: SELECT COUNT(*) SQL COUNT(DISTINCT column_name) Syntax: The COUNT(DISTINCT column_name) function returns the number of distinct values of the specified column: SELECT COUNT(DISTINCT column_name)

SELECT SUM() Function SQL SUM() Syntax: SELECT SUM(column_name) FROM table_name; Below is a selection from the "OrderDetails" table: OrderDetailID OrderID ProductID Quantity 1 10248 11 12 2 42 10 3 72 5 4 10249 14 9 51 40

SQL SUM() Function The following SQL statement finds the sum of all the "Quantity" fields for the "OrderDetails" table: SELECT SUM(Quantity) AS TotalItemsOrdered FROM OrderDetails; http://www.w3schools.com/sql/trysql.asp?filename=trysql_func_sum