sql

Is it possible to index "SELECT domain_name from domains WHERE 'x.example.com' like domain_name" where domain_name contains "%.example.com"

Hey, I am trying to create a system to match wildcard domain names. Although the query above will work for me is it possible to index it at all? I don't have access to the DB to do any clever stuff as I am using Heroku to host my app but I can add indexes easy enough? Thanks! Edit: I would like to match wildcard domain names in my da...

In SQL, how do I get all rows where a column's value is the lowest in the table?

I am a newbie to SQL, I am using this query to look for the minimum value in the field weight of my table. SELECT product_id, MIN(weight) FROM table WHERE 1; It does show one field with the min value, but only one? But I have many products with the same minimum weight. Is there a way I could specify that I need to show a...

variable column names in sql table valued function

Suppose that I am using a sql server to keep track of all my personal expenses and that I am tagging everything with a date and a category. This allows me to do things like aggregate monthly expenses per category and look at the last several months of expenses with each category as a row and the most recent months as columns. What ...

Preventing SQL Injection In Ibatis

Is there anything built into Ibatis that helps prevent SQL Injection attacks? I'm not looking for a list of ways outside of Ibatis to do this, and am just wondering if Ibatis has anything to prevent SQL Injection. ...

Does every table really need an auto-incrementing artificial primary key?

Almost every table in every database I've seen in my 7 years of development experience has an auto-incrementing primary key. Why is this? If I have a table of U.S. states where each state where each state must have a unique name, what's the use of an auto-incrementing primary key? Why not just use the state name as the primary key? Seems...

sql using aliases

I seem to be having some trouble grasping the concept of using AS to create an alias, and then using that alias later. Here is an example of a very simple query that I get an error when I run: SELECT IP, (SELECT server_ip as IP FROM table1 WHERE start_time BETWEEN @startdate AND @enddate Group By...

How to get the next available primary key from a SqlCe DB ?

How can I get the next available primary key from my table? The primary key is a numeric. I'm using C#. ...

Oracle 10g Connect By Prior - Performance Issues

I have the following SQL statement: SELECT CONNECT_BY_ROOT ANIMAL_ID "ORIGINAL_ANIMAL" , ANIMAL_ID, LINE_ID, SIRE_ANIMAL_ID, DAM_ANIMAL_ID, LEVEL -1 "LEVEL" FROM ANIMALS START WITH ANIMAL_ID IN( '2360000002558' ) CONNECT BY ((PRIOR SIRE_ANIMAL_ID = ANIMAL_ID and LEVEL < 5) OR (PRIOR DAM_ANIMAL_ID = ANIMAL_ID AND LEVEL < ...

How to count results within results in MySQL?

I have a table of items which I'm getting from a few different online stores, like Ebay/Amazon, etc. Before today, I wanted to simply group each item based on the year it was made and the manufacturer who made it. So, the results would look something like this total year manufacturer 100 1999 man_a 32 2002 man_b Now, I wan...

Getting a list of items from one table and adding aggregated ratings from another

I currently have two tables, one with documents, and another with ratings doc_id | doc_groupid | doc_name | doc_time and then rating_id | rating_docid | rating_score where rating_score is either -1 or 1. What I need to do is have a single query that retrieves every column in the document table WHERE groupid = #, but also has colum...

Help with select statement with one to many relationship

what is the best way to write a select statement with two table that have a one to many relation ship? I have a table called broker with these fields id companyname then another table called brokerContact that has a foreign key to the broker table with these fields id brokerid contact name phone How can I write a select statement...

PostgreSQL, checking date relative to "today"

Hello All, Was wondering if someone could assist with some Postgres. I have a table which has a column called mydate which is a postgres date type. I want to do something like: SELECT * FROM MyTable WHERE mydate > [Today-1year] I've never used Postgres before and I'm sure I just need to know the name of some functions- I'll gladly l...

How to Take Part of a String using One of the T-SQL Text Functions

I have the following snippet: CONVERT(varchar, FLOOR(ROUND((DATEDIFF(minute, shiftStart.timeEntered, shiftEnd.timeEntered) - DATEDIFF(minute, lunchStart.timeEntered, lunchEnd.timeEntered)) * 1.0 / 60, 2, 1))) + ':' + CONVERT(varchar, ROUND(ROUND(DATEDIFF(minute, shiftStart.timeEntered, shif...

How would you create the data in table1 given the data in table2?

I know SQL pretty well for joins and queries, but when it comes to this I have no idea. Can someone take a crack at this, please? How would you create the data in table1 given the data in table2? Write an SQL query. Table1 (csv): order_id1, prod_id1 order_id1, prod_id2 order_id1, prod_id3 order_id1, prod_id4 order_id1, prod_id4 Tabl...

To what degree can effective indexing overcome performance issues with VERY large tables?

So, it seems to me like a query on a table with 10k records and a query on a table with 10mil records are almost equally fast if they are both fetching roughly the same number of records and making good use of simple indexes(auto increment, record id type indexed field). My question is, will this extend to a table with close to 4 billio...

query on finding youngest person

Hi, I am writing a query on Mysql . I have to find the youngest customer and in my database , I have 2 persons with same dob. I am doing it in a way that first it checks all the conditions and then order by dob desc, limit 1. It gives me only 1 result. If I set the limit 2, I get the second person but setting limit according to database...

Simple SQL DAL for 4-5 queries

I have to gather data from a sql database to populate a ning map i am creating. I only need data from one table in the database. The table has company data in it, name country state, email, website, etc. What is the best way to do this? I am doing my bing map using .net so i was thinking of just gathering all my data in code behind i...

Limitations of GROUP BY

Disclaimer: I'm an SQL newb and this is for a class, but I could really use a poke in the right direction. I've got these three tables: student(_sid_, sname, sex, age, year, gpa) section(_dname_, _cno_, _sectno_, pname) enroll(_sid_, grade, _dname_, _cno_, _sectno_) (primary keys denoted by underscores) I'm trying to write an Oracle...

How optimal is this function

I'm about to implement this function to calculate some numbers. CREATE FUNCTION [dbo].[funLookupFTE] (@PFID int) RETURNS VARCHAR(20) AS BEGIN DECLARE @NumberOfFTE AS VARCHAR(20) SET @NumberOfFTE = (SELECT SUM(CASE WHEN Hours <= 20 THEN 0.5 WHEN Hours > 20 THEN 1 END) AS FTECount FROM tblPractitioners ...

How would you construct this SQL query? (MySQL)

Let assume we have these tables: product product_id product_name category category_id category_name product_in_category product_in_category_id product_id category_id how would you get all products that are not in specific category in the product_in_category table (without duplicates). In other words, all products t...