L e a r n S q l select where

Similar documents
MySQL. Prof.Sushila Aghav

SQL is a standard language for accessing and manipulating databases.

Jarek Szlichta

SQL Tutorial. Gerson Almeida SQL. Tutorial / 15:09 1 (24)

Advance SQL: SQL Performance Tuning. SQL Views

Introduction to SQL Server 2005/2008 and Transact SQL

CHAPTER: 4 ADVANCE SQL: SQL PERFORMANCE TUNING (12 Marks)

30. Structured Query Language (SQL)

CSC Web Programming. Introduction to SQL

Unit 1 - Chapter 4,5

Assignment Grading Rubric

MIS2502: Review for Exam 2. JaeHwuen Jung

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

RESTRICTING AND SORTING DATA

How to use SQL to work with a MySQL database

11. Introduction to SQL

Intro to Structured Query Language Part I

MIS2502: Review for Exam 2. Jing Gong

Stored Procedures and Functions. Rose-Hulman Institute of Technology Curt Clifton

QUETZALANDIA.COM. 5. Data Manipulation Language

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

Database Management Systems,

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

Data Manipulation Language (DML)

Instructor: Craig Duckett. Lecture 03: Tuesday, April 3, 2018 SQL Sorting, Aggregates and Joining Tables

Department of Computer Science & IT

SQL. Structured Query Language (SQL ISO/CEI 9075:2011) focused mainly on Oracle 11g r2 for Data Science. Vincent ISOZ V6.0 r ouuid 1839

Aga Non-Government Institute for Computer Science

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

SQL Data Manipulation Language. Lecture 5. Introduction to SQL language. Last updated: December 10, 2014

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

Exact Numeric Data Types

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

COUNT Function. The COUNT function returns the number of rows in a query.

Oracle 1Z Oracle Database 11g SQL Fundamentals I. Download Full Version :

Structure Query Language (SQL)

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

Microsoft Exam Transition Your MCTS on SQL Server 2008 to MCSA: SQL Server 2012, Part 1 Version: 7.8 [ Total Questions: 183 ]

Data Modelling and Databases Exercise dates: March 22/March 23, 2018 Ce Zhang, Gustavo Alonso Last update: March 26, 2018.

Limit Rows Selected. Copyright 2008, Oracle. All rights reserved.

Assignment 6: SQL III Solution

Database Programming with SQL

SQL Commands & Mongo DB New Syllabus

DATABASE MANAGEMENT SYSTEMS

How to use SQL to create a database

CS 275 Winter 2011 Problem Set 3

Database Management Systems by Hanh Pham GOALS

Unit 6A. SQL: Overview and Coding SQL Commands

Structured Query Language (SQL)

1Z0-007 ineroduction to oracle9l:sql

Assignment 6: SQL III

Why Relational Databases? Relational databases allow for the storage and analysis of large amounts of data.

Database Systems. Answers

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

Lesson 2. Data Manipulation Language

Microsoft MOS- Using Microsoft Office Access Download Full Version :

Advanced SQL GROUP BY Clause and Aggregate Functions Pg 1

DUE: CD_NUMBER TITLE PRODUCER YEAR 97 Celebrate the Day R & B Inc Holiday Tunes for All Tunes are US 2004

Relational Database Development

The query language for relational databases Jef De Smedt

Data Manipulation Language Bag. 3

1) Introduction to SQL

Unit 27 Web Server Scripting Extended Diploma in ICT

A subquery is a nested query inserted inside a large query Generally occurs with select, from, where Also known as inner query or inner select,

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

Mysql interview questions and answers

Intermediate SQL: Aggregated Data, Joins and Set Operators

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

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

SQL. Structured Query Language

1Z Oracle Database 11g - SQL Fundamentals I Exam Summary Syllabus Questions

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

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

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

IMPORTING DATA IN PYTHON I. Introduction to relational databases

Database Wizard Guide. i-net Designer

CMP-3440 Database Systems

Using Microsoft Access

Lab # 4 Hands-On. DDL and DML Advance SQL Statements Institute of Computer Science, University of Tartu, Estonia

Lecture 06. Fall 2018 Borough of Manhattan Community College

Activant Solutions Inc. SQL 2005: Basic Data Manipulation

Structured Query Language

Review. Objec,ves. Example Students Table. Database Overview 3/8/17. PostgreSQL DB Elas,csearch. Databases

IT Certification Exams Provider! Weofferfreeupdateserviceforoneyear! h ps://

MIS2502: Data Analytics SQL Getting Information Out of a Database. Jing Gong

chapter 2 G ETTING I NFORMATION FROM A TABLE

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

ALTER TABLE Statement

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Working with Columns, Characters and Rows. Copyright 2008, Oracle. All rights reserved.

Exam Name: Querying Microsoft SQL Server 2012

Structure Query Language (SQL)

SQL-Structured Query Language BY : SHIVENDER KUMAR BHARDWAJ PGT-TAFS

Introduction to Databases and SQL

Exam code: Exam name: Database Fundamentals. Version 16.0

Consuming and Manipulating Data O BJECTIVES

Exam #1 Review. Zuyin (Alvin) Zheng

CSE 344 Introduction to Data Management. Section 2: More SQL

Insertions, Deletions, and Updates

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

Transcription:

L e a r n S q l The select statement is used to query the database and retrieve selected data that match the criteria that you specify. Here is the format of a simple select statement: select "column1" [,"column2",etc] from "tablename" [where "condition"]; [] = optional Conditional selections used in the where clause: = Equal > Greater than < Less than >= Greater than or equal <= Less than or equal <> Not equal to LIKE *See note below 1. Display everyone's first name and their age for everyone that's in table. SELECT first, age FROM empinfo; 2. Display the first name, last name, and city for everyone that's not from Payson. SELECT first, last, city from empinfo WHERE city <>'Payson'; Note: Payson is string data enclosed in between 3. Display all columns for everyone that is over 40 years old. SELECT * from empinfo WHERE age > 40; 4. Display the first and last names for everyone whose last name ends in an "ay". SELECT first, last from empinfo WHERE last LIKE '%ay'; Note: % is wild card character that matches any possible character before ay 5. Display all columns for everyone whose first name equals "Mary". SELECT * from empinfo WHERE first = 'Mary'; Note: * is wild card character to select all fields 6. Display all columns for everyone whose first name contains "Mary". SELECT * from empinfo WHERE first LIKE '%Mary%';

The create table statement is used to create a new table. Here is the format of a simple create table statement: create table "tablename" ("column1" "data type", "column2" "data type", "column3" "data type"); CREATE TABLE employee (first varchar(15), last varchar(20), age number(3), address varchar(30), city varchar(20), state varchar(20)); CREATE TABLE Persons (ID int, LastName varchar(255), FirstName varchar(255), Age int, PRIMARY KEY (ID)); CREATE TABLE Persons (ID int PRIMARY KEY, LastName varchar(255), FirstName varchar(255), Age int); The insert statement is used to insert or add a row of data into the table. INSERT into "tablename" (first_column,...last_column) VALUES (first_value,...last_value); INSERT into myemployees_ts0211 (firstname, lastname, title, age, salary) values ('Jonie', 'Weber', 'Secretary', 28, 19500.00); INSERT into employees (first, last, age, address, city, state) values ('Luke', 'Duke', 45, '2130 Boars Nest', 'Hazard Co', 'Georgia'); The update statement is used to update or change records that match a specified criteria. This is accomplished by carefully constructing a where clause. update "tablename" set "columnname" = "newvalue" [,"nextcolumn" = "newvalue2"...] where "columnname" OPERATOR "value" [and or "column" OPERATOR "value"]; [] = optional 1. Jonie Weber just got married to Bob Williams. She has requested that her last name be updated to Weber-Williams.

UPDATE myemployees_ts0211 SET lastname='weber-williams' where firstname='jonie' and lastname= 'Weber'; 2. Dirk Smith's birthday is today, add 1 to his age. UPDATE myemployees_ts0211 SET age=age+1 WHERE firstname='dirk' and lastname='smith'; 3. All secretaries are now called "Administrative Assistant". Update all titles accordingly. update myemployees_ts0211 set title = 'Administrative Assistant' where title = 'Secretary'; 4. Everyone that's making under 30000 are to receive a 3500 a year raise. update myemployees_ts0211 set salary = salary + 3500 where salary < 30000; 5. Everyone that's making over 33500 are to receive a 4500 a year raise. update myemployees_ts0211 set salary = salary + 4500 where salary > 33500; 6. All "Programmer II" titles are now promoted to "Programmer III". update myemployees_ts0211 set title = 'Programmer III' where title = 'Programmer II' 7. All "Programmer" titles are now promoted to "Programmer II". update myemployees_ts0211 set title = 'Programmer II' where title = 'Programmer' Note: Be careful when updating records. If you omit the WHERE clause, ALL records will be updated UPDATE Customers SET ContactName='Juan'; To change the salaries of all the employees, the query would be, UPDATE employee SET salary = salary + (salary * 0.2); Deleting Records The delete statement is used to delete records or rows from the table. delete from "tablename" where "columnname" OPERATOR "value" [and or "column" OPERATOR "value"]; [ ] = optional 1. Jonie Weber-Williams just quit, remove her record from the table: DELETE FROM myemployees_ts0211 WHERE lastname = 'Weber-Williams'; 2. It's time for budget cuts. Remove all employees who are making over 70000 dollars. delete from myemployees_ts0211 where salary > 70000; Note: if you leave off the where clause, all records will be deleted! The drop table command is used to delete a table and all rows in the table. DROP TABLE myemployees_ts0211; The GROUP BY clause will gather all of the rows together that contain data in the specified column(s) and will allow aggregate functions to be performed on the one or more columns. SELECT state, count(state) FROM customers GROUP BY state;

ORDER BY is an optional clause which will allow you to display the results of your query in a sorted order (either ascending order or descending order) based on the columns that you specify to order by. SELECT lastname, firstname, city FROM customers ORDER BY lastname; SELECT lastname, firstname, city FROM customers ORDER BY lastname DESC; SELECT item, price FROM items_ordered WHERE price > 10.00 ORDER BY price ASC; Note: Ascending by default The AND operator can be used to join two or more conditions in the WHERE clause. SELECT employeeid, firstname, lastname, title, salary FROM employee_info WHERE salary >= 45000.00 AND title = 'Programmer'; The OR operator can be used to join two or more conditions in the WHERE clause also 1. Select the item and price of all items that start with the letters 'S', 'P', or 'F'. SELECT item, price FROM items_ordered WHERE (item LIKE 'S%') OR (item LIKE 'P%') OR (item LIKE 'F%'); Joins allow you to link data from two or more tables together into a single query result--from one single SELECT statement. An ideal database would have two tables: 1. One for keeping track of your customers 2. And the other to keep track of what they purchase: "Customer_info" table: customer_number firstname lastname address city state zip "Purchases" table: customer_number date item price SELECT customer_info.firstname, customer_info.lastname, purchases.item FROM customer_info INNER JOIN purchases ON customer_info.customer_number = purchases.customer_number; SELECT employee_info.employeeid, employee_info.lastname, employee_sales.comission FROM employee_info, employee_sales WHERE employee_info.employeeid = employee_sales.employeeid; The INNER JOIN keyword selects records that have matching values in both tables. Below is a selection from the "Orders" table: OrderID CustomerID EmployeeID OrderDate ShipperID 10308 2 7 1996-09-18 3 10309 37 3 1996-09-19 1 10310 77 8 1996-09-20 2

And a selection from the "Customers" table: CustomerID CustomerName ContactName Address City PostalCode Country 1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany 2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico The following SQL statement selects all orders with customer information: SELECT Orders.OrderID, Customers.CustomerName FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID; ALTER TABLE To create a PRIMARY KEY constraint on the "ID" column when the table is already created, use the following SQL: ALTER TABLE Persons ADD PRIMARY KEY (ID); ALTER TABLE Persons DROP PRIMARY KEY;