Giter VIP home page Giter VIP logo

sql-commands's Introduction

Learn SQL commands with sample queries

1. Prerequisites

1.1. Pull and run SQL Server Docker image

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=Luiscoco123456" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2022-latest

1.2. Install SQL Server Management Studio (SSMS)

1.3. Create the database, tables and data for executing the samples

Let's create a simple database scenario to illustrate SQL commands and queries. We will work with a database for a bookstore, which includes tables for Books, Authors, and Sales

I'll provide you with the SQL commands to create the database, tables, and insert some data. Then, I'll show you a few queries to interact with this data

Step 1: Create Database

First, let's create a database called Bookstore.

CREATE DATABASE Bookstore;

Step 2: Create Tables

Now, we will create three tables: Authors, Books, and Sales.

Authors Table

CREATE TABLE Authors (
    AuthorID INT PRIMARY KEY,
    Name VARCHAR(100),
    Country VARCHAR(50)
);

Books Table

CREATE TABLE Books (
    BookID INT PRIMARY KEY,
    Title VARCHAR(100),
    AuthorID INT,
    Price DECIMAL(10,2),
    YearPublished INT, -- Changed from YEAR to INT
    FOREIGN KEY (AuthorID) REFERENCES Authors(AuthorID)
);

Sales Table

CREATE TABLE Sales (
    SaleID INT PRIMARY KEY,
    BookID INT,
    QuantitySold INT,
    SaleDate DATE,
    FOREIGN KEY (BookID) REFERENCES Books(BookID)
);

Insert Data

Let's insert some sample data into these tables.

Insert Authors

INSERT INTO Authors (AuthorID, Name, Country) VALUES
(1, 'George Orwell', 'United Kingdom'),
(2, 'J.K. Rowling', 'United Kingdom'),
(3, 'Harper Lee', 'United States');

Insert Books

INSERT INTO Books (BookID, Title, AuthorID, Price, YearPublished) VALUES
(1, '1984', 1, 8.99, '1949'),
(2, 'Animal Farm', 1, 6.99, '1945'),
(3, 'Harry Potter and the Sorcerer''s Stone', 2, 10.99, '1997'),
(4, 'To Kill a Mockingbird', 3, 7.99, '1960');

Insert Sales

INSERT INTO Sales (SaleID, BookID, QuantitySold, SaleDate) VALUES
(1, 1, 500, '2023-01-01'),
(2, 2, 300, '2023-02-01'),
(3, 3, 800, '2023-03-01'),
(4, 4, 400, '2023-04-01');

We can see the tables relation in the following diagram

image

2. SQL commands samples

2.1. SQL JOINS

image

sql-commands's People

Contributors

luiscoco avatar

Watchers

 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.