SQL. Structured Query Language

Similar documents
CSC Web Programming. Introduction to SQL

11. Introduction to SQL

Unit 27 Web Server Scripting Extended Diploma in ICT

SQL stands for Structured Query Language. SQL lets you access and manipulate databases

Introduction to SQL. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Alper VAHAPLAR

Chapter-14 SQL COMMANDS

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Unit 1 - Chapter 4,5

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Chapter 13 : Informatics Practices. Class XI ( As per CBSE Board) SQL Commands. New Syllabus Visit : python.mykvs.in for regular updates

SQL Commands & Mongo DB New Syllabus

MySQL. Prof.Sushila Aghav

Databases - 4. Other relational operations and DDL. How to write RA expressions for dummies


INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

Sql Server Syllabus. Overview

30. Structured Query Language (SQL)

1) Introduction to SQL

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

Exam code: Exam name: Database Fundamentals. Version 16.0

SQL Joins and SQL Views

SQL. A C P K Siriwardhana MSc, BSc in Computer Science FIRST COURSE

SQL functions fit into two broad categories: Data definition language Data manipulation language

Exact Numeric Data Types

STRUCTURED QUERY LANGUAGE (SQL)

Structured Query Language (SQL) lab syllabus 4 th science. SQL is used to communicate with a database. it is the standard language for relational

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

Querying Data with Transact SQL

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

IBM Software Group Information Management Software. The Informix Detective Game (Student Handout)

Oracle Database 11g: SQL and PL/SQL Fundamentals

G64DBS Database Systems. Lecture 7 SQL SELECT. The Data Dictionary. Data Dictionaries. Different Sections of SQL (DDL) Different Sections of SQL (DCL)

IT360: Applied Database Systems. SQL: Structured Query Language DDL and DML (w/o SELECT) (Chapter 7 in Kroenke) SQL: Data Definition Language

SQL Data Definition Language: Create and Change the Database Ray Lockwood

tablename ORDER BY column ASC tablename ORDER BY column DESC sortingorder, } The WHERE and ORDER BY clauses can be combined in one

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

SQL DATA MANIPULATION. Prepared By: Dr. Vipul Vekariya.

Relational Database Language

Database Processing: Fundamentals, Design, and Implementation

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

Types. Inner join ( Equi Joins ) Outer(left, right, full) Cross. Prepared By : - Chintan Shah & Pankti Dharwa 2

Database Foundations. 6-3 Data Definition Language (DDL) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Jarek Szlichta

Chapter 9: Working with MySQL

RESTRICTING AND SORTING DATA

Chapter # 7 Introduction to Structured Query Language (SQL) Part II

Test Bank for Database Processing Fundamentals Design and Implementation 13th Edition by Kroenke

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Private Institute of Aga NETWORK DATABASE LECTURER NIYAZ M. SALIH

Database Management Systems,

Full file at

QUETZALANDIA.COM. 5. Data Manipulation Language

DATABASE MANAGEMENT SYSTEMS

MTA Database Administrator Fundamentals Course

Structured Query Language

SQL. SQL Data Manipulation: Queries

Database Systems. Bence Molnár

Retrieving Data Using the SQL SELECT Statement. Copyright 2009, Oracle. All rights reserved.

Structured Query Language (SQL) Part A. KSE 521 Topic 10 Mun Yi

CS 327E Lecture 2. Shirley Cohen. January 27, 2016

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

Database &.NET Basics: Take what you know about SQL and apply that to SOQL, SOSL, and DML in Apex.

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

Database Foundations. 6-9 Joining Tables Using JOIN. Copyright 2014, Oracle and/or its affiliates. All rights reserved.

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database 10g: Introduction to SQL

UNIT III INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL)

DATABASE MANAGEMENT SYSTEMS PREPARED BY: ENGR. MOBEEN NAZAR

Database Management Systems by Hanh Pham GOALS

SQL Simple Queries. Chapter 3.1 V3.01. Napier University

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

Activant Solutions Inc. SQL 2005: Basic Data Manipulation

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Simple Quesries in SQL & Table Creation and Data Manipulation

2) SQL includes a data definition language, a data manipulation language, and SQL/Persistent stored modules. Answer: TRUE Diff: 2 Page Ref: 36

Database Management Systems,

Advanced SQL Tribal Data Workshop Joe Nowinski

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

Chapter 1 SQL and Data

Data Base Lab. The Microsoft SQL Server Management Studio Part-3- By :Eng.Alaa I.Haniy.

Oracle 1Z MySQL 5.6 Developer.

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

SQL. SQL Functions. SQL Aggregate Functions. SQL Scalar functions. SQL Aggregate Functions. The AVG() Function

EE221 Databases Practicals Manual

UNIT-3 RELATIONAL DATABASE MANAGEMENT SYSTEM

Today Learning outcomes LO2

1. Data Definition Language.

Institute of Aga. Microsoft SQL Server LECTURER NIYAZ M. SALIH

Chapter 4: SQL Basics

MySQL Introduction. By Prof. B.A.Khivsara

SQL. Char (30) can store ram, ramji007 or 80- b

Group A: Assignment No 2

ITCertMaster. Safe, simple and fast. 100% Pass guarantee! IT Certification Guaranteed, The Easy Way!

Graphical Joins in More Detail

Querying Data with Transact-SQL

Transcription:

SQL Structured Query Language 1 Başar Öztayşi 2017

SQL SQL is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database systems. SQL works with database programs like MS Access, DB2, Informix, MS SQL Server, Oracle, Sybase, etc. 2 Başar Öztayşi 2017

There are different versions of the SQL language, but they must support the same major keywords in a similar manner (such as SELECT, UPDATE, DELETE, INSERT, WHERE, and others) with SQL standards Most of the SQL database programs also have their own extensions in addition to the SQL standard! 3 Başar Öztayşi 2017

SQL Database Tables Person LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Bohçacı Berke Çeşme Sokak 4/1 İzmit Sungur Ayşegül Tepe Sokak 8/23 Balıkesir 4 Başar Öztayşi 2017

SQL Queries With SQL, we can query a database and have a result set returned. A query like this: SELECT LastName FROM Person Gives a result set like this: LastName Kiraz Bohçacı Sungur 5 Başar Öztayşi 2017

SQL can be divided into two parts: The Data Manipulation Language (DML) The Data Definition Language (DDL). 6 Başar Öztayşi 2017

Data Manipulation Language SQL (Structured Query Language) is a syntax for executing queries. But the SQL language also includes a syntax to update, insert, and delete records. SELECT - extracts data from a database table UPDATE - updates data in a database table DELETE - deletes data from a database table INSERT INTO - inserts new data into a database table 7 Başar Öztayşi 2017

Data Definition Language The Data Definition Language (DDL) part of SQL permits database tables to be created or deleted. We can also define indexes (keys), specify links between tables, and impose constraints between database tables. The most important DDL statements in SQL are: CREATE TABLE - creates a new database table ALTER TABLE - alters (changes) a database table DROP TABLE - deletes a database table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index 8 Başar Öztayşi 2017

SQL The SELECT Statement 9 Başar Öztayşi 2017

SQL The SELECT Statement The SELECT statement is used to select data from a table. The tabular result is stored in a result table (called the result-set). Syntax SELECT column_name(s) FROM table_name 10 Başar Öztayşi 2017

LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Bohçacı Berke Çeşme Sokak 4/1 İzmit Sungur Ayşegül Tepe Sokak 8/23 Balıkesir To select the columns named "LastName" and "FirstName", use a SELECT statement like this: SELECT LastName, FirstName FROM Person LastName Kiraz Bohçacı Sungur FirstName Meltem Berke Ayşegül 11 Başar Öztayşi 2017

Select All Columns To select all columns from the "Person" table, use a * symbol instead of column names, like this: SELECT * FROM Person LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Bohçacı Berke Çeşme Sokak 4/1 İzmit Sungur Ayşegül Tepe Sokak 8/23 Balıkesir 12 Başar Öztayşi 2017

Select Distinct Statement The DISTINCT keyword is used to return only distinct (different) values. The SELECT statement returns information from table columns. But what if we only want to select distinct elements? With SQL, all we need to do is to add a DISTINCT keyword to the SELECT statement: Syntax SELECT DISTINCT column_name(s) FROM table_name 13 Başar Öztayşi 2017

Using Distinct Keyword To select ALL values from the column named "Company" we use a SELECT statement like this: SELECT Company FROM Orders Orders Company OrderNumber SONY 3412 ACME 5464 VESTEL 5876 ACME 3425 Company SONY ACME VESTEL ACME 14 Başar Öztayşi 2017

Note that "W3Schools" is listed twice in the result-set. To select only DIFFERENT values from the column named "Company" we use a SELECT DISTINCT statement like this: SELECT DISTINCT Company FROM Orders Orders Company OrderNumber SONY 3412 ACME 5464 VESTEL 5876 ACME 3425 Company SONY ACME VESTEL 15 Başar Öztayşi 2017

Select All Columns The WHERE clause is used to specify a selection criterion. The WHERE Clause To conditionally select data from a table, a WHERE clause can be added to the SELECT statement. Syntax SELECT column FROM table WHERE column operator value 16 Başar Öztayşi 2017

Operators With the WHERE clause, the following operators can be used: Operator Description = Equal <> Not equal > Greater than < Less than >= Greater than or equal <= Less than or equal BETWEEN LIKE IN Between an inclusive range Search for a pattern If you know the exact value you want to return for at least one of the columns 17 Başar Öztayşi 2017

Using the WHERE Clause To select only the persons living in the city "İzmit", we add a WHERE clause to the SELECT statement: SELECT * FROM Person WHERE City= İzmit' LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Bohçacı Berke Çeşme Sokak 4/1 İzmit Sungur Ayşegül Tepe Sokak 8/23 Balıkesir Bohçacı Nilgün Kahya Sokak 3/65 İzmit LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Bohçacı Berke Çeşme Sokak 4/1 İzmit Bohçacı Nilgün Kahya Sokak 3/65 İzmit 18 Başar Öztayşi 2017

Using Quotes SQL uses single quotes around text values Numeric values should not be enclosed in quotes. For Text Valuıes This is correct: SELECT * FROM Person WHERE FirstName= Ayşe This is wrong: SELECT * FROM Person WHERE FirstName=Ayşe For Numeric Values This is correct: SELECT * FROM Person WHERE Year=1985 This is wrong: SELECT * FROM Person WHERE Year= 1985 19 Başar Öztayşi 2017

The LIKE Condition The LIKE condition is used to specify a search for a pattern in a column. Syntax SELECT column FROM table WHERE column LIKE pattern A "%" sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern. 20 Başar Öztayşi 2017

Using LIKE The following SQL statement will return persons with first names that start with an 'O': SELECT * FROM Person WHERE FirstName LIKE 'O%' The following SQL statement will return persons with first names that end with an 'a': SELECT * FROM Person WHERE FirstName LIKE '%a' 21 Başar Öztayşi 2017

Using LIKE 2 The following SQL statement will return persons with first names that contain the pattern 'la': SELECT * FROM Person WHERE FirstName LIKE '%la%' 22 Başar Öztayşi 2017

SQL The INSERT INTO Statement 23 Başar Öztayşi 2017

The INSERT INTO Statement The INSERT INTO statement is used to insert new rows into a table. Syntax INSERT INTO table_name VALUES (value1, value2,...) You can also specify the columns for which you want to insert data: INSERT INTO table_name (column1, column2,...) VALUES (value1, value2,...) 24 Başar Öztayşi 2017

Insert a New Row LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit INSERT INTO Person VALUES ( Arslan', Hüseyin', Karga Sok 3/23', Balıkesir') LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir 25 Başar Öztayşi 2017

Insert Data in Specified Columns LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir And This SQL statement: INSERT INTO Person (LastName, Address) VALUES ( Kibar', 'Terzi Sok 1/3') LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir Kibar Terzi Sok 1/3 26 Başar Öztayşi 2017

SQL The UPDATE Statement 27 Başar Öztayşi 2017

The Update Statement The UPDATE statement is used to modify the data in a table. Syntax UPDATE table_name SET column_name = new_value WHERE column_name = some_value 28 Başar Öztayşi 2017

Update one Column in a Row LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir Kibar Terzi Sok 1/3 We want to add a first name to the person with a last name of "Kibar": UPDATE Person SET FirstName = Nehar' WHERE LastName = Kibar' LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir Kibar Nehar Terzi Sok 1/3 29 Başar Öztayşi 2017

Update several Columns in a Row LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir Kibar Nehar Terzi Sok 1/3 We want to change the address and add the name of the city: UPDATE Person SET Address = Keresteci Sok 1/7', City = İzmit WHERE LastName = Kibar' LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir Kibar Nehar Keresteci Sok 1/7 İzmit 30 Başar Öztayşi 2017

SQL The Delete Statement 31 Başar Öztayşi 2017

The Delete Statement The DELETE statement is used to delete rows in a table. Syntax DELETE FROM table_name WHERE column_name = some_value 32 Başar Öztayşi 2017

Delete a Row LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir Kibar Nehar Terzi Sok 1/3 İzmit "Nehar Kibar" is going to be deleted: DELETE FROM Person WHERE LastName = Kibar' LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir 33 Başar Öztayşi 2017

LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir Kibar Nehar Terzi Sok 1/3 İzmit LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir Kibar Nehar Terzi Sok 1/3 34 Başar Öztayşi 2017

Create Table Creating a basic table involves naming the table and defining its columns and each column's data type CREATE TABLE table_name( column1 datatype, column2 datatype, column3 datatype,... columnn datatype, PRIMARY KEY( one or more columns ) ); 35 Başar Öztayşi 2017

Create Table CREATE TABLE Student ( ST_ID int PRIMARY KEY, Name CHAR(255), Course CHAR(255), Marks INTEGER, Grade CHAR(255), Phone INTEGER, Present YESNO ) 36 Başar Öztayşi 2017

Drop Table The SQL DROP TABLE statement is used to remove a table definition and all the data, indexes, triggers, constraints and permission specifications for that table. DROP TABLE table_name; 37 Başar Öztayşi 2017

Alter Table The SQL ALTER TABLE command is used to add, delete or modify columns in an existing table. You should also use the ALTER TABLE command to add and drop various constraints on an existing table. 38 Başar Öztayşi 2017

Add Column The basic syntax of an ALTER TABLE command to add a New Column in an existing table is as follows. ALTER TABLE table_name ADD column_name datatype; ALTER TABLE Student ADD SurName CHAR(255); 39 Başar Öztayşi 2017

Drop Column The basic syntax of an ALTER TABLE command to DROP COLUMN in an existing table is as follows. ALTER TABLE table_name DROP COLUMN column_name; ALTER TABLE Student Drop Column SurName; 40 Başar Öztayşi 2017

Modify Column The basic syntax of an ALTER TABLE command to change the DATA TYPE of a column in a table is as follows. ALTER TABLE table_name MODIFY COLUMN column_name datatype; 41 Başar Öztayşi 2017

Not Null Constraint The basic syntax of an ALTER TABLE command to add a NOT NULL constraint to a column in a table is as follows. ALTER TABLE table_name MODIFY column_name datatype NOT NULL; 42 Başar Öztayşi 2017

Inner Join SELECT table1.column1, table2.column2... FROM table1 INNER JOIN table2 ON table1.common_field = table2.common_field The INNER JOIN creates a new result table by combining column values of two tables (table1 and table2) based upon the joinpredicate. The query compares each row of table1 with each row of table2 to find all pairs of rows which satisfy the join-predicate. When the join-predicate is satisfied, column values for each matched pair of rows of A and B are combined into a result row. 43 Başar Öztayşi 2017

Left Join The SQL LEFT JOIN returns all rows from the left table, even if there are no matches in the right table. This means that if the ON clause matches 0 (zero) records in the right table; the join will still return a row in the result, but with NULL in each column from the right table. This means that a left join returns all the values from the left table, plus matched values from the right table or NULL in case of no matching join predicate SELECT table1.column1, table2.column2... FROM table1 LEFT JOIN table2 ON table1.common_field = table2.common_field; 44 Başar Öztayşi 2017

Right Join SELECT table1.column1, table2.column2... FROM table1 RIGHT JOIN table2 ON table1.common_field = table2.common_field; The SQL RIGHT JOIN returns all rows from the right table, even if there are no matches in the left table. This means that if the ON clause matches 0 (zero) records in the left table; the join will still return a row in the result, but with NULL in each column from the left table. This means that a right join returns all the values from the right table, plus matched values from the left table or NULL in case of no matching join predicate. 45 Başar Öztayşi 2017