Writing pseudo-sql statement - mysql

Today I passed an interview and thez ask me this :
Write pseudo SQl statements to create database tables to store the products of a basic websho. Each product has a name, a price, a creation date ans may belong to several categories. Categories have a name and a flag to indicate whether the category is private or public.
Also:
Write a SQL query to find the list of products that belong to more than 5 public categories.
My knowledge of sql is pretty limited, i just pass 2 hours searching on the web for pseudo-sql statements without result.
Can someone explain me what it is and eventually respond to the answers so i'll know ? (interview already fail so you dont do my homework ahahah)
Thanks!

product(pro_id, pro_name, price, date)
pro_id is the Primary key
category(cat_id, cat_name, cat_flag, cat_type)
cat_id s the primary key
procat(pro_id, cat_id)
pro_id, cat_id are primary key together
Query:
select pro_id from procat where cat_id in (select cat_id from category where cat_type = 'public') group by pro_id having count(*)>2

Related

MySQL query returned 0 rows

I have a task to make a database only in MySQL. I made 11 tables and connected them via foreign keys. I tried to make a simple query in order to return name and lastname of the patient in his diagnose, but I always get only a header with the first and last name and analysis.
The patient's table has nameID, name, last name, ID serial number, date of birth and so on, but I wanted only name and last name for the test query.
The second table I joined is analysis, which has analysisID, patientID, doctorID, hospitalID, diagnosis and so on.
My query is like this:
SELECT pat.name, pat.lastname
FROM patient pat
JOIN analysis a ON pat.patientID = a.patientID
group by a.analysisID
order by pat.lastname
This query returns 0 rows. Please help, I am new at mySQL. I read a lot of tutorials, read posts here about this problem and I still didn't find a solution.
I assume that you want to eliminate any duplicate analysisID's for the same person with the use of group by. If so, you could use the following:
Select a.analysisID, pat.name, pat.lastname
from patient pat, analysis a
where pat.patientID = a.patientID
group by a.analysisID, pat.name, pat.lastname
What the above query will do is return only one record when the analysisID, name and lastname are all the same.

SQL 2008 pull in Child records

I am trying to return data for someone in the following format from a SQL 2008 query -
ProductID, ProductTitle
ServiceID, ServiceTitle
ServiceID, ServiceTitle
ProductID2, ProductTitle2
ServiceID, ServiceTitle
ProductID3, ProductTitle3...
So the product table lists the products and then the product service table will have several services assigned to one product ID. Is the above even possible? Or something similar?
It is very possible, and it's a good practice when you're just beginning SQL,
this is how your database/table structure should be:
t_Products : ID, Name, Price
t_ProductService:
ID
serviceName
productID
the ID in t_Products should be primary key and
the productID in t_ProductService should be the foreign key of the ID in t_Products
Edited:
You need to use join, there are a lot of join in SQL,
SELECT * FROM t_ProductService ps
INNER JOIN t_Products p
ON ps.ProductsID = p.ID

query to search related fields

I have the tables and i am trying to get the active name(s) of a product by using the tracking Id.
These are the tables i have:
tblactive: tblProductcode: tblproduct tblCV
client_name client_name client_name Tracking_id
Product_name product_name product_name product_code
active_name product_code status
color
I want to be able to search for the active name when i key in the tracking id:
I have been thinking about this for awhile but just cannot figure out... probably monday blues.
This is the sql query i have currently have:
SELECT tblActive.active_name, tblActive.l4
FROM (tblActive INNER JOIN tblProductCode ON
(tblActive.Client_Name = tblProductCode.Client_Name)
AND (tblActive.Product_Name = tblProductCode.Product_Name))
INNER JOIN tblCV ON tblProductCode.Product_Code = tblCV.Product_Code
WHERE (((tblcv.product_code)=[forms]![frmCVSwabRequest]![cboProduct_code]));
This query just returns a blank.
Why dont you just make a unique primary key named tblactive_id for table tblactive
and add it to the tblCV table. Then simply run this query,
SELECT tblactive.active_name FROM tblactive, tblCV WHERE
tblactive.tblactive_id = tblCV.tblactive;
I think you need to redesign your database tables properly.

Microsoft Access checking account DB with Categories, Subcategories, and dependent fields

I understand - and have created - a self-referencing table that contains Categories & Subcategories. SO, you get a table that looks like this:
Categories Table:
id, primary Key autoNumber
category, text
parentID, foreign Key, number
categories.ID has a one-to-many relationship with categories.parentID
==Here is what I'm having a really tough time understanding==
I also have a Transactions table used to track a purchase, and two columns to record the Category and its Subcategory. Think "Automobile: Gasoline", or "Healthcare: Prescriptions".
Transactions Table:
id, primaryKey, autoNumber
payee, shortText
Category, foreign key, (number, comboBox)
Subcategory (number, comboBox)
categories.ID has a one-to-many relationship with Transactions.Category
I can't find the appropriate way to populate Category and Subcategory in the Transactions table.
So far, I was able to pull all distinct categories from the Categories Table with a SQL statement similar to the example below. (Any Category with a NULL parentID is considered a root category).
SELECT *
FROM Categories
WHERE (((Categories.[parentID]) Is Null))
ORDER BY Categories.category DESC;
My problem is finding a way to get Transactions.Subcategory to list only the subcategories associated with the chosen root category.
I don't know how to do this in Access.
Any tips, advice, etc., would be very much appreciated.

Mysql query, need some help. XOR operation

I have a table Books with isbn and name of books, and I have a table orders with oisbn(foreign key) and ocid (foreign key, customer ID of he who has bought the book).
A
I want to list the cids of those customers who have ordered only ocid=123 or ocid = 567,NOT both. Kind of like XOR.
How do I accomplish this in mysql? I've been thinking for a long time and I can't figure it out.
SELECT
foo
FROM
bar
WHERE ocid IN (123, 567)
GROUP BY customerId
HAVING COUNT(*) = 1