Giter VIP home page Giter VIP logo

revkevtest2-createview's Introduction

SQL Views

A VIEW in SQL is a virtual table that was created based on a SQL statement that was predefined. For example, lets say we had the following table:

site_user
id firstname lastname age
1 'Steve' 'Garcia' 23
2 'Alexa' 'Smith' 40
3 'Steve' 'Jones' 29
4 'Brandon' 'Smith' 50
5 'Adam' 'Jones' 61

If we wanted to retrieve all the records with the firstname 'Steve', we can do that with the following statement: SELECT * FROM site_user WHERE firstname = 'Steve';

steve_view
id firstname lastname age
1 'Steve' 'Garcia' 23
3 'Steve' 'Jones' 29

What we can do is put this virtual table in a view, so we can query data based on the virtual table above instead of the actual table in the database.

The syntax for creating a view is as follows:

CREATE VIEW view_name AS sql_statement;

So the syntax for creating the 'steve view' table above would be:

CREATE VIEW steve_view AS SELECT * FROM site_user WHERE firstname = 'Steve';

This is beneficial because we can now execute SQL queries on this view instead of the entire table. For example if I wanted to get the average ages of all the steves, I can do that with this new view and the aggregate function AVG() like so: SELECT AVG(age) from steve_view;

Additional Reference Material

Lab

For the following problems, consider the site_user table below:

site_user
id firstname lastname age
1 'Steve' 'Garcia' 23
2 'Alexa' 'Smith' 40
3 'Steve' 'Jones' 29
4 'Brandon' 'Smith' 50
5 'Adam' 'Jones' 61

Problem 1

Create a view called "firstname_lastname" from the site_user table that only has the firstname and lastname columns.

NOTE: This view should NOT contain the id and age columns.

NOTE2: please write the SQL statement on one line for this lab.

revkevtest2-createview's People

Contributors

revkevtest2 avatar kevinchilds avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.